aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2021-08-07 01:34:18 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2021-08-07 01:34:18 +0100
commitce45fdeb454766aca81fd0b3eec22ef8d9fe52dd (patch)
tree5fef471cd9b0785ce1c1ab3cc6e11fdb3968d010
parentc9d51119672fa4703f855351b279156143231606 (diff)
downloadperlweeklychallenge-club-ce45fdeb454766aca81fd0b3eec22ef8d9fe52dd.tar.gz
perlweeklychallenge-club-ce45fdeb454766aca81fd0b3eec22ef8d9fe52dd.tar.bz2
perlweeklychallenge-club-ce45fdeb454766aca81fd0b3eec22ef8d9fe52dd.zip
- Added Haskell solution by Ulrich Rieke.
-rw-r--r--challenge-124/ulrich-rieke/haskell/ch-2.hs36
1 files changed, 36 insertions, 0 deletions
diff --git a/challenge-124/ulrich-rieke/haskell/ch-2.hs b/challenge-124/ulrich-rieke/haskell/ch-2.hs
new file mode 100644
index 0000000000..5e79c460c1
--- /dev/null
+++ b/challenge-124/ulrich-rieke/haskell/ch-2.hs
@@ -0,0 +1,36 @@
+module Challenge124_2
+ where
+import qualified Data.Set as Set
+import Data.List ( minimumBy , sort )
+
+combinations :: Int -> [a] -> [[a]]
+combinations 0 _ = [[]]
+combinations n xs = [ xs !! i : x | i <- [0..(length xs ) - 1 ] ,
+x <- combinations (n - 1 ) ( drop ( i + 1 ) xs ) ]
+
+findComplement :: [Int] -> [Int] -> [Int]
+findComplement numbers combi = Set.toList ( Set.fromList numbers Set.\\
+Set.fromList combi )
+
+solution :: [Int] -> String
+solution list = "Subset 1 = " ++ (show $ sort $ fst pair) ++ " " ++
+"Subset 2 = " ++ ( show $ sort $ snd pair )
+where
+ pair :: ([Int] , [Int] )
+ pair = minimumBy myFunc listPairs
+ l :: Int
+ l = length list
+ allCombis :: [[Int]]
+ allCombis = if even l then combinations ( div l 2 ) list else
+ (combinations ( div l 2 ) list) ++ ( combinations ( div l 2 + 1 ) list)
+ listPairs :: [( [Int] , [Int] )]
+ listPairs = map (\combi -> ( combi , findComplement list combi ) )
+ allCombis
+ myFunc :: ([Int] , [Int] ) -> ([Int] , [Int] ) -> Ordering
+ myFunc pair1 pair2
+ | abs ( (sum $ fst pair1) - (sum $ snd pair1) ) < abs ( (sum $ fst pair2
+ ) - (sum $ snd pair2) ) = LT
+ | abs ( (sum $ fst pair1) - (sum $ snd pair1) ) == abs ( (sum $ fst pair2
+ ) - (sum $ snd pair2) ) = EQ
+ | abs ( (sum $ fst pair1) - (sum $ snd pair1) ) > abs ( (sum $ fst pair2
+ ) - (sum $ snd pair2) ) = GT