diff options
| -rwxr-xr-x | challenge-007/stuart-little/haskell/ch-1.hs | 6 | ||||
| -rwxr-xr-x | challenge-007/stuart-little/haskell/ch-2.hs | 28 |
2 files changed, 34 insertions, 0 deletions
diff --git a/challenge-007/stuart-little/haskell/ch-1.hs b/challenge-007/stuart-little/haskell/ch-1.hs new file mode 100755 index 0000000000..70c993827a --- /dev/null +++ b/challenge-007/stuart-little/haskell/ch-1.hs @@ -0,0 +1,6 @@ +#!/usr/bin/env runghc + +-- run <script> + +main = do + mapM_ print $ let digSum x = sum $ map (\d-> read [d]::Int) $ show x in (0:) $ filter (\x -> mod x (digSum x) == 0) [1..50] diff --git a/challenge-007/stuart-little/haskell/ch-2.hs b/challenge-007/stuart-little/haskell/ch-2.hs new file mode 100755 index 0000000000..4ff8c03e56 --- /dev/null +++ b/challenge-007/stuart-little/haskell/ch-2.hs @@ -0,0 +1,28 @@ +#!/usr/bin/env runghc + +-- run <script> <word1> <word2> <path-to-file> + +import System.Environment (getArgs,) +import Data.List (delete,) +import Data.List.Extra (minimumOn,notNull,) +import Math.TreeFun.Tree (boolToInt,) +import Control.Monad (liftM,) + +dist1 :: Eq a => [a] -> [a] -> Bool +dist1 xs ys = ((length xs) == (length ys)) && ((==) 1 $ sum $ map boolToInt $ zipWith (/=) xs ys) + +shortestLadder :: String -> String -> [String] -> [String] +shortestLadder w1 w2 ws + |(notElem w1 ws) || (notElem w2 ws) =[] + |w1 == w2 =[w1] + |otherwise = let + wsshort = delete w1 ws + l = map (w1:) $ filter (notNull) $ map (\w -> shortestLadder w w2 wsshort) $ filter (dist1 w1) wsshort + in + if (null l) then [] + else (minimumOn length l) + +main = do + (w1:w2:file:_) <- getArgs + ws <- liftM words $ readFile file + putStrLn $ unwords $ shortestLadder w1 w2 ws |
