diff options
| -rwxr-xr-x | challenge-058/stuart-little/haskell/ch-1.hs | 25 | ||||
| -rwxr-xr-x | challenge-058/stuart-little/haskell/ch-2.hs | 27 |
2 files changed, 52 insertions, 0 deletions
diff --git a/challenge-058/stuart-little/haskell/ch-1.hs b/challenge-058/stuart-little/haskell/ch-1.hs new file mode 100755 index 0000000000..ca6d372eb1 --- /dev/null +++ b/challenge-058/stuart-little/haskell/ch-1.hs @@ -0,0 +1,25 @@ +#!/usr/bin/env runghc + +-- run <script> <two space-separated strings> + +import Data.List.Split (split,onSublist,) +import Data.List.Utils (replace,) +import Data.Function (on,) +import System.Environment (getArgs,) +import Test.Tasty.Options (safeRead,) + +zeroPad :: Int -> String -> String +zeroPad n w = (replicate (n-length w) '0') ++ w + +padIfInt :: Int -> String -> String +padIfInt n w = case (safeRead::String->Maybe Int) w of + Nothing -> w + Just _ -> zeroPad n w + +prepStringForComp :: Int -> String -> [String] +prepStringForComp n w = map (padIfInt n) $ concat . (map (split (onSublist "!"))) $ split (onSublist ".") $ replace "_" "!" w + +main = do + (s1:s2:_) <- getArgs + let n = length s1 + length s2 + print $ (\x->x-1).fromEnum $ (compare `on` (prepStringForComp n)) s1 s2 diff --git a/challenge-058/stuart-little/haskell/ch-2.hs b/challenge-058/stuart-little/haskell/ch-2.hs new file mode 100755 index 0000000000..a396c812dd --- /dev/null +++ b/challenge-058/stuart-little/haskell/ch-2.hs @@ -0,0 +1,27 @@ +#!/usr/bin/env runghc + +-- run <script> + +import Data.List (sortOn,) +import System.Environment (getArgs,) + +posNext :: Maybe [a] -> (a,Int) -> Maybe [a] +posNext Nothing _ = Nothing +posNext (Just xs) (y,n) + |length ls < n = Nothing + |otherwise = Just (ls ++ [y] ++ rs) where + (ls,rs) = splitAt n xs + +ordByPrec :: Ord a => [a] -> [Int] -> Maybe [a] +ordByPrec heights taller = foldl posNext (Just []) $ reverse $ sortOn fst $ zip heights taller + +pprnt :: Show a => [a] -> String +pprnt = unwords . map show + +main = do + let heights = [27,21,37,4,19,52,23,64,1,7,51,17,24,50,3,2,34,40,47,20,8,56,14,16,42,38,62,53,31,41,55,59,48,12,32,61,9,60,46,26,58,25,15,36,11,44,63,28,5,54,10,49,57,30,29,22,35,39,45,43,18,6,13,33] + taller = [6,41,1,49,38,12,1,0,58,47,4,17,26,1,61,12,29,3,4,11,45,1,32,5,9,19,1,4,28,12,2,2,13,18,19,3,4,1,10,16,4,3,29,5,49,1,1,24,2,1,38,7,7,14,35,25,0,5,4,19,10,13,4,12] + + putStrLn $ "\nList of heights: \n" ++ (pprnt heights) + putStrLn $ "\nNumber of taller people in front of each customer: \n" ++ (pprnt taller) + putStrLn $ "\nQueue order: \n" ++ (maybe "Impossible to sort." pprnt $ ordByPrec heights taller) |
