aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMyoungjin JEON <jeongoon@gmail.com>2020-10-22 22:48:34 +1100
committerMyoungjin JEON <jeongoon@gmail.com>2020-10-22 22:48:34 +1100
commit9a510abb7e11b23abd13ca572d161e6506bb127c (patch)
tree15b7f3eced73afffce18d46b570cb3045168b27f
parentf0749fdd43fc1c98170ca312777fb999ef674329 (diff)
downloadperlweeklychallenge-club-9a510abb7e11b23abd13ca572d161e6506bb127c.tar.gz
perlweeklychallenge-club-9a510abb7e11b23abd13ca572d161e6506bb127c.tar.bz2
perlweeklychallenge-club-9a510abb7e11b23abd13ca572d161e6506bb127c.zip
[ch-083/jeongoon] Haskell Solution added
-rw-r--r--challenge-083/jeongoon/haskell/Combinations.hs25
-rw-r--r--challenge-083/jeongoon/haskell/ch-1.hs20
-rw-r--r--challenge-083/jeongoon/haskell/ch-2.hs53
3 files changed, 98 insertions, 0 deletions
diff --git a/challenge-083/jeongoon/haskell/Combinations.hs b/challenge-083/jeongoon/haskell/Combinations.hs
new file mode 100644
index 0000000000..f8c5324f43
--- /dev/null
+++ b/challenge-083/jeongoon/haskell/Combinations.hs
@@ -0,0 +1,25 @@
+{- Copyright (c) 2020 JEON Myoungjin <jeongoon@g... > -}
+
+module Combinations
+ ( combinations
+ ) where
+
+combinations :: [a] -> Int -> [[a]]
+combinations [] _ = []
+combinations (m:ms) 1 = [m] : (combinations ms 1)
+combinations [_] 2 = []
+combinations [e,f] 2 = sequence [ [e],[f] ]
+combinations (m:ms) 2 = sequence [ [m], ms ] ++ (combinations ms 2)
+combinations mls n =
+ case totalLen `compare` n of
+ LT -> []
+ EQ -> [mls]
+ _ -> [ let leaders = map (mls!!) ids
+ in leaders ++ followers |
+ ids <- combinations [ 0 .. room ] n',
+ let skipCount = (last ids) + 1,
+ followers <- (combinations (drop skipCount mls) 2) ]
+ where
+ totalLen = length mls
+ room = totalLen - 2
+ n' = n - 2
diff --git a/challenge-083/jeongoon/haskell/ch-1.hs b/challenge-083/jeongoon/haskell/ch-1.hs
new file mode 100644
index 0000000000..1d108ae629
--- /dev/null
+++ b/challenge-083/jeongoon/haskell/ch-1.hs
@@ -0,0 +1,20 @@
+import System.Environment
+import System.Exit
+
+{- test with:
+runhaskell ch-1.hs "Perl W e e k l y Challenge" # output: 6
+-}
+main = do
+ args <- getArgs;
+ if length args /= 1
+ then die "Usage: runhaskell ch-1.hs <a string with 3 or more words"
+ else let ws = words (args !! 0)
+ wl = length ws
+ in
+ if wl < 3 then die "we need 3 words at least"
+ else (putStrLn.
+ show.
+ length.
+ foldr1 (++).
+ take (wl-2).
+ drop 1) ws
diff --git a/challenge-083/jeongoon/haskell/ch-2.hs b/challenge-083/jeongoon/haskell/ch-2.hs
new file mode 100644
index 0000000000..c64170400a
--- /dev/null
+++ b/challenge-083/jeongoon/haskell/ch-2.hs
@@ -0,0 +1,53 @@
+import System.Environment
+import System.Exit
+import Data.Char (isNumber)
+import Data.Maybe (isJust, catMaybes)
+import Data.List (sum)
+import Combinations (combinations)
+
+
+{- test with:
+ runhaskell ch-2.hs 12 7 4 # answer: 12
+ runhaskell ch-2.hs 12 7 4 5 6 9 20 12 7 4 5 6 9 20 9 4 2 1 13 8 # answer: 6
+ # or in order to get faster result, you can compile and execute:
+ ghc ch-2.hs
+ ./ch-2 12 7 4 5 6 9 20 12 7 4 5 6 9 20 9 4 2 1 13 8 # answer: 6
+-}
+
+answerFlipArray :: (Integral a) => [a] -> Int
+answerFlipArray nums
+ | totalSum == 1 = 0
+ | otherwise = answerWith totalSum totalLen numCombis
+ where
+ totalSum = sum nums
+ totalLen = length nums
+ halfLen = totalLen `div` 2
+ numCombis = (foldr1 (++) .
+ map (combinations nums))
+ [ 1 .. halfLen ]
+
+ answerWith _ minElems [] = minElems
+ answerWith minSum minElems (aCombi:otherCombis) =
+ case (positiveSum `compare` minSum) of
+ LT -> answerWith positiveSum newElems otherCombis
+ EQ -> answerWith minSum (min newElems minElems) otherCombis
+ GT -> answerWith minSum minElems otherCombis
+ where
+ len = length aCombi
+ sm = sum aCombi
+ sm' = totalSum - sm
+ (positiveSum, newElems) =
+ case (sm `compare` sm') of
+ LT -> ( sm' - sm, len )
+ EQ -> ( 0, (min len (totalLen - len)) )
+ GT -> ( sm - sm', (totalLen - len) )
+
+main = do
+ (catMaybes.map (\nStr ->
+ if (all isNumber nStr) then Just(read nStr :: Int)
+ else Nothing )) `fmap` getArgs
+ >>= (\nums ->
+ if length nums < 1 then
+ die "Usage: runhaskell ch-2.hs <natural num> ..."
+ else
+ putStrLn $ show ( answerFlipArray nums ) )