aboutsummaryrefslogtreecommitdiff
path: root/challenge-042
diff options
context:
space:
mode:
author冯昶 <fengchang@novel-supertv.com>2021-01-18 16:47:22 +0800
committer冯昶 <fengchang@novel-supertv.com>2021-01-18 16:47:22 +0800
commit7021a6ee78be5c0a1bc1a30db583004a88fc7a15 (patch)
tree92663512339b43f573537719a568b138f3c7bd57 /challenge-042
parent8d8f3d2effddf8b25dab04cb31f39973a34c57e4 (diff)
parent754e6dcd79dde5387229a23ae7eca35d70eac4a3 (diff)
downloadperlweeklychallenge-club-7021a6ee78be5c0a1bc1a30db583004a88fc7a15.tar.gz
perlweeklychallenge-club-7021a6ee78be5c0a1bc1a30db583004a88fc7a15.tar.bz2
perlweeklychallenge-club-7021a6ee78be5c0a1bc1a30db583004a88fc7a15.zip
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-042')
-rwxr-xr-xchallenge-042/stuart-little/haskell/ch-1.hs12
-rwxr-xr-xchallenge-042/stuart-little/haskell/ch-2.hs20
2 files changed, 32 insertions, 0 deletions
diff --git a/challenge-042/stuart-little/haskell/ch-1.hs b/challenge-042/stuart-little/haskell/ch-1.hs
new file mode 100755
index 0000000000..937f58adde
--- /dev/null
+++ b/challenge-042/stuart-little/haskell/ch-1.hs
@@ -0,0 +1,12 @@
+#!/usr/bin/env runghc
+
+-- run <script>
+
+convToBase b nr
+ |nr < b =[nr]
+ |otherwise = (convToBase b high) ++ [low] where
+ (high,low) = divMod nr b
+
+main = do
+ let l = [1..50]
+ mapM_ putStrLn $ map (\(x,y) -> "Decimal: " ++ (show x) ++ " -- Octal: " ++ (concat $ map show y)) $ zip l $ map (convToBase 8) l
diff --git a/challenge-042/stuart-little/haskell/ch-2.hs b/challenge-042/stuart-little/haskell/ch-2.hs
new file mode 100755
index 0000000000..87996c398d
--- /dev/null
+++ b/challenge-042/stuart-little/haskell/ch-2.hs
@@ -0,0 +1,20 @@
+#!/usr/bin/env runghc
+
+-- run <script> <quoted string of parentheses or nothing; if you provide nothing a string will be generated randomly>
+
+import System.Environment (getArgs,)
+import Data.List (inits,)
+import Data.List.Extra (lastDef,headDef,)
+import System.Random (getStdGen,randomRIO,randomRs,)
+
+validate str =
+ let f '(' = 1; f ')' = -1; g x = map sum $ (map.map) f $ inits x
+ in ((lastDef 0 $ g str) == 0) && (all (0<=) $ g str)
+
+main = do
+ args <- getArgs
+ nrchars <- randomRIO (1::Int,50)
+ gen <- getStdGen
+ let strng = headDef (take (nrchars) (randomRs ('(',')') gen)) args
+ putStrLn ("Your string: " ++ strng)
+ putStrLn $ show $ validate strng