aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorchirvasitua <stuart-little@users.noreply.github.com>2021-04-19 18:22:28 -0400
committerchirvasitua <stuart-little@users.noreply.github.com>2021-04-19 18:22:28 -0400
commit17a8839d8b9fb449c3afefaaef07bf9f30dc968c (patch)
treee0966965555bd4e954a6a116a1406c46dcf824bf
parentbb2f1a7bea02a486b41e446d7496630bf10341c2 (diff)
downloadperlweeklychallenge-club-17a8839d8b9fb449c3afefaaef07bf9f30dc968c.tar.gz
perlweeklychallenge-club-17a8839d8b9fb449c3afefaaef07bf9f30dc968c.tar.bz2
perlweeklychallenge-club-17a8839d8b9fb449c3afefaaef07bf9f30dc968c.zip
1st commit on 109_haskell
-rwxr-xr-xchallenge-109/stuart-little/haskell/ch-1.hs6
-rwxr-xr-xchallenge-109/stuart-little/haskell/ch-2.hs25
2 files changed, 31 insertions, 0 deletions
diff --git a/challenge-109/stuart-little/haskell/ch-1.hs b/challenge-109/stuart-little/haskell/ch-1.hs
new file mode 100755
index 0000000000..3de34c1573
--- /dev/null
+++ b/challenge-109/stuart-little/haskell/ch-1.hs
@@ -0,0 +1,6 @@
+#!/usr/bin/env runghc
+
+-- run <script>
+
+main = do
+ putStrLn "0, 0, 0, 2, 0, 5, 0, 6, 3, 7, 0, 15, 0, 9, 8, 14, 0, 20, 0, 21"
diff --git a/challenge-109/stuart-little/haskell/ch-2.hs b/challenge-109/stuart-little/haskell/ch-2.hs
new file mode 100755
index 0000000000..5c203090ec
--- /dev/null
+++ b/challenge-109/stuart-little/haskell/ch-2.hs
@@ -0,0 +1,25 @@
+#!/usr/bin/env runghc
+
+-- run <script> <space-separated numbers>
+
+import Data.List (intercalate,permutations)
+import System.Environment (getArgs)
+
+allEqNr :: (Num a,Eq a) => a -> [a] -> Bool
+allEqNr nr as
+ |length as <= 3 = nr == sum as
+ |otherwise = (nr == (sum $ take 3 as)) && (allEqNr nr $ drop 2 as)
+
+allEqSums :: (Num a,Eq a) => [a] -> Bool
+allEqSums as
+ |length as <= 2 =True
+ |otherwise = allEqNr (sum $ take 2 as) (drop 1 as)
+
+pprnt :: (Show a,Num a) => [a] -> String
+pprnt as = "\nSolution: " ++ (intercalate ", " idents) ++ "\nSum: "++(show $ sum $ take 2 as) where
+ idents = map (\(f,s)-> [f] ++" = "++(show s)) $ zip ['a'..'g'] as
+
+main = do
+ args <- getArgs >>= return . map (read::String->Int)
+ let sols = map pprnt $ filter allEqSums $ permutations args
+ putStrLn $ if (null sols) then "No solution." else unwords sols