aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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
+