diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-09-12 20:01:29 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-09-12 20:01:29 +0100 |
| commit | b5126aad694761aee94fcfeb8ab0d3c8a395ff1c (patch) | |
| tree | fb4c4c2edc09f9856e6104a9cb1812a9384d278e | |
| parent | ab7a0ad445316b978b4ad6832f0392877ab6100b (diff) | |
| download | perlweeklychallenge-club-b5126aad694761aee94fcfeb8ab0d3c8a395ff1c.tar.gz perlweeklychallenge-club-b5126aad694761aee94fcfeb8ab0d3c8a395ff1c.tar.bz2 perlweeklychallenge-club-b5126aad694761aee94fcfeb8ab0d3c8a395ff1c.zip | |
- Added Haskell solutions by Ulrich Rieke.
| -rw-r--r-- | challenge-077/ulrich-rieke/haskell/ch-1.hs | 19 | ||||
| -rw-r--r-- | challenge-077/ulrich-rieke/haskell/ch-2.hs | 52 |
2 files changed, 71 insertions, 0 deletions
diff --git a/challenge-077/ulrich-rieke/haskell/ch-1.hs b/challenge-077/ulrich-rieke/haskell/ch-1.hs new file mode 100644 index 0000000000..13a0ad797a --- /dev/null +++ b/challenge-077/ulrich-rieke/haskell/ch-1.hs @@ -0,0 +1,19 @@ +module Challenge077 + where +import Data.List ( nub, subsequences , intercalate) + +fibonacci :: Int -> Int +fibonacci 0 = 0 +fibonacci 1 = 1 +fibonacci n = fibonacci ( n - 2 ) + fibonacci ( n - 1 ) + +findList :: Int -> [ Int ] +findList n = nub $ takeWhile ( < n ) $ map fibonacci [1..n] + +convert :: [Int] -> String +convert list = (intercalate " + " $ map show list) ++ " = " ++ ( show $ +sum list ) + +solution :: Int -> [String] +solution n = map convert $ filter (( n == ) . sum ) $ subsequences +$ findList n diff --git a/challenge-077/ulrich-rieke/haskell/ch-2.hs b/challenge-077/ulrich-rieke/haskell/ch-2.hs new file mode 100644 index 0000000000..37800a1e76 --- /dev/null +++ b/challenge-077/ulrich-rieke/haskell/ch-2.hs @@ -0,0 +1,52 @@ +module Challenge077_2 + where +import Control.Applicative +import Data.List ( (!!) , (\\) , findIndices ) + +--given a row or column index in the array, which is the allowable range +--of neighbouring rows or colums ? +findRange :: (Int , Int ) -> Int -> [Int] +findRange ( lowerLimit , upperLimit ) value + |value == lowerLimit = [value , value + 1] + |value > lowerLimit && value < upperLimit = [value - 1 , value , value + + 1 ] + |value == upperLimit = [ value - 1 , value ] + |otherwise = [] + +--which are the indexes of neighbours to a 'x' in the array +--look at the possible ranges of rows and columns and subtract from that +--the index of the x +findNeighbours :: [String] -> Int -> Int -> [(Int , Int)] +findNeighbours array row column = ((,) <$> (findRange (0 , length array - 1) +row) <*> ( findRange (0 , (length $ head array) - 1 ) column )) \\ + [(row , column)] + +findChar :: [String] -> Int -> Int -> Char +findChar array row column = ( array !! row ) !! column + +--find the x'es in every row +find_x_Indices :: [String] -> [[Int]] +find_x_Indices array = map (\row -> findIndices ('x' == ) row ) array + +--find the neighbouring indices in every line +findAllNeighbourIndices :: [String] -> [[Int]] -> [[(Int , Int)]] +findAllNeighbourIndices array xindices = concat $ map (\p -> map(\i -> +findNeighbours array ( fst p ) i ) (snd p) ) $ zip [0, 1 ..] xindices + +--find neighbouring strings +findStrings :: [String] -> [[(Int , Int)]] -> [String] +findStrings array foundIndices = map (\list -> map (\p -> findChar +array ( fst p ) ( snd p ) ) list ) foundIndices + + +solution :: [String] -> Int +solution array = length $ filter condition allNeighbourStrings + where + allXIndices :: [[Int ]] + allXIndices = find_x_Indices array + allNeighbourIndices :: [[(Int, Int)]] + allNeighbourIndices = findAllNeighbourIndices array allXIndices + allNeighbourStrings :: [String] + allNeighbourStrings = findStrings array allNeighbourIndices + condition :: String -> Bool + condition s = all ('o' == ) s |
