diff options
| -rwxr-xr-x | challenge-111/stuart-little/haskell/ch-1.hs | 39 | ||||
| -rwxr-xr-x | challenge-111/stuart-little/haskell/ch-2.hs | 16 |
2 files changed, 55 insertions, 0 deletions
diff --git a/challenge-111/stuart-little/haskell/ch-1.hs b/challenge-111/stuart-little/haskell/ch-1.hs new file mode 100755 index 0000000000..da0af6c7ff --- /dev/null +++ b/challenge-111/stuart-little/haskell/ch-1.hs @@ -0,0 +1,39 @@ +#!/usr/bin/env runghc + +-- run <script> + +import System.Environment (getArgs) + +binSearch :: Int -> [Int] -> Int +binSearch = binSearchOffset 0 + +binSearchOffset :: Int -> Int -> [Int] -> Int +binSearchOffset offset needle xs + |null xs = -1 + |length xs == 1 = if needle == head xs then offset else -1 + | needle == xs !! mid = offset+mid + |otherwise = if needle < xs !! mid then binSearchOffset offset needle low else binSearchOffset (offset+mid+1) needle high where + mid = div (length xs) 2 + low = take mid xs + high = drop (mid+1) xs + +pprnt :: [Int] -> Int -> String +pprnt xs needle = "Found " ++ (show needle) ++ "?\n" ++ (show zero1) where + zero1 = if binSearch needle xs < 0 then 0 else 1 + +inArr :: [[Int]] +inArr = [ + [ 1, 2, 3, 5, 7 ], + [ 9, 11, 15, 19, 20 ], + [ 23, 24, 25, 29, 31 ], + [ 32, 33, 39, 40, 42 ], + [ 45, 47, 48, 49, 50 ] + ] + +toSearch :: [Int] +toSearch = [1,35,39,100] + +main = do + putStrLn "Array:" + putStrLn . unlines . map unwords . (map.map) show $ inArr + putStrLn . unlines . map (pprnt (concat inArr)) $ toSearch diff --git a/challenge-111/stuart-little/haskell/ch-2.hs b/challenge-111/stuart-little/haskell/ch-2.hs new file mode 100755 index 0000000000..8f6d59faaa --- /dev/null +++ b/challenge-111/stuart-little/haskell/ch-2.hs @@ -0,0 +1,16 @@ +#!/usr/bin/env runghc + +-- run <script> <path-to-dict-file, one word per line> + +import Control.Monad (liftM) +import Data.Char (toLower) +import Data.List.Extra (groupSortOn) +import Data.List.Ordered (isSorted) +import System.Environment (getArgs) + +longestSorted :: [String] -> [String] +longestSorted wrds = head . groupSortOn (((-1)*) . length) . filter (isSorted . map toLower) $ wrds + +main = do + wrds <- getArgs >>= (liftM lines) . readFile . head + putStrLn . unlines . longestSorted $ wrds |
