aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorchirvasitua <stuart-little@users.noreply.github.com>2021-01-10 21:01:41 -0500
committerchirvasitua <stuart-little@users.noreply.github.com>2021-01-10 21:01:41 -0500
commit5817c989b47250f7b32859a042a14e72417e87b3 (patch)
tree391626c5c4a2fce41b829c3b6ca1a476489b434b
parentbba37b96ac80b23bd9b268b92eeb237f8dcedbfa (diff)
downloadperlweeklychallenge-club-5817c989b47250f7b32859a042a14e72417e87b3.tar.gz
perlweeklychallenge-club-5817c989b47250f7b32859a042a14e72417e87b3.tar.bz2
perlweeklychallenge-club-5817c989b47250f7b32859a042a14e72417e87b3.zip
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
+