diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-05-04 07:01:01 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-05-04 07:01:01 +0100 |
| commit | fc1db3c344ee8fe0addbc856543fb0b17cd8dda0 (patch) | |
| tree | 6efaead4e1b981fcbbdb1f6a9ae4c60fed09aa94 /challenge-111/stuart-little | |
| parent | 81dfad7326475457f317a5ea92ff863c544ef974 (diff) | |
| parent | dbbfa1198b03cdac2227f7378ec40921922a10f8 (diff) | |
| download | perlweeklychallenge-club-fc1db3c344ee8fe0addbc856543fb0b17cd8dda0.tar.gz perlweeklychallenge-club-fc1db3c344ee8fe0addbc856543fb0b17cd8dda0.tar.bz2 perlweeklychallenge-club-fc1db3c344ee8fe0addbc856543fb0b17cd8dda0.zip | |
Merge pull request #4014 from stuart-little/stuart-little_111_haskell
1st commit on 111_haskell
Diffstat (limited to 'challenge-111/stuart-little')
| -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 |
