aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-01-11 02:22:51 +0000
committerGitHub <noreply@github.com>2021-01-11 02:22:51 +0000
commit1b89f76a548c9d03152d52dccbd46871708fb6c7 (patch)
tree1001597384398df86176f0640dd8affc0366ef8e
parent4bb25ad6c2244aae29c4eb448866573fa71175fc (diff)
parent5817c989b47250f7b32859a042a14e72417e87b3 (diff)
downloadperlweeklychallenge-club-1b89f76a548c9d03152d52dccbd46871708fb6c7.tar.gz
perlweeklychallenge-club-1b89f76a548c9d03152d52dccbd46871708fb6c7.tar.bz2
perlweeklychallenge-club-1b89f76a548c9d03152d52dccbd46871708fb6c7.zip
Merge pull request #3216 from stuart-little/stuart-little_002_haskell
1st commit on 002_haskell
-rwxr-xr-xchallenge-002/stuart-little/haskell/ch-1.hs9
-rwxr-xr-xchallenge-002/stuart-little/haskell/ch-2.hs37
2 files changed, 46 insertions, 0 deletions
diff --git a/challenge-002/stuart-little/haskell/ch-1.hs b/challenge-002/stuart-little/haskell/ch-1.hs
new file mode 100755
index 0000000000..156989082c
--- /dev/null
+++ b/challenge-002/stuart-little/haskell/ch-1.hs
@@ -0,0 +1,9 @@
+#!/usr/bin/env runghc
+
+-- run <script> <integer>
+
+import System.Environment (getArgs,)
+
+main = do
+ (nr:_) <- getArgs
+ putStrLn $ show (read nr::Int)
diff --git a/challenge-002/stuart-little/haskell/ch-2.hs b/challenge-002/stuart-little/haskell/ch-2.hs
new file mode 100755
index 0000000000..686b217cc8
--- /dev/null
+++ b/challenge-002/stuart-little/haskell/ch-2.hs
@@ -0,0 +1,37 @@
+#!/usr/bin/env runghc
+
+{-
+run <script> <number> <option>
+
+<option> means 35 to convert number to base 35 or 10 to convert backwards
+-}
+
+import System.Environment (getArgs,)
+import Data.List (lookup,)
+import Data.Maybe (fromMaybe,)
+import Data.Tuple (swap,)
+import Data.Char (toUpper,)
+
+convToListBase :: Int -> Int -> [Int]
+convToListBase base nr
+ |nr < base =[nr]
+ |otherwise = (convToListBase base dv) ++ [md]
+ where (dv,md) = divMod nr base
+
+convFromListBase :: Int -> [Int] -> Int
+convFromListBase base xs = sum $ zipWith (*) (reverse xs) $ iterate (base *) 1
+
+numListToStr :: [(Int,Char)] -> [Int] -> String
+numListToStr dict xs = map (fromMaybe '/') $ map (flip lookup dict) xs
+
+strToNumList :: [(Int,Char)] -> String -> [Int]
+strToNumList dict str = map (fromMaybe 0) $ map (flip lookup (map swap dict)) str
+
+main = do
+ (nr:opt:_) <- getArgs
+ let dict = zip [0..34] (['0'..'9'] ++ ['A'..'Z'])
+ res
+ |opt=="35" = numListToStr dict $ convToListBase 35 (read nr::Int)
+ |otherwise = show $ convFromListBase 35 $ strToNumList dict $ map toUpper nr
+ putStrLn res
+