aboutsummaryrefslogtreecommitdiff
path: root/challenge-087
diff options
context:
space:
mode:
authorchirvasitua <stuart-little@users.noreply.github.com>2021-01-17 17:22:17 -0500
committerchirvasitua <stuart-little@users.noreply.github.com>2021-01-17 17:22:17 -0500
commita28e1e04e8b18e59701bae3e9b00cf19a82487e4 (patch)
treedb1ade3dd1649267fab3595366968aae3e8b8ba5 /challenge-087
parent7b93df13a7335a14a6b2f32c35c395587c5d4c05 (diff)
downloadperlweeklychallenge-club-a28e1e04e8b18e59701bae3e9b00cf19a82487e4.tar.gz
perlweeklychallenge-club-a28e1e04e8b18e59701bae3e9b00cf19a82487e4.tar.bz2
perlweeklychallenge-club-a28e1e04e8b18e59701bae3e9b00cf19a82487e4.zip
1st commit on 087_haskell
Diffstat (limited to 'challenge-087')
-rwxr-xr-xchallenge-087/stuart-little/haskell/ch-1.hs15
-rwxr-xr-xchallenge-087/stuart-little/haskell/ch-2.hs23
2 files changed, 38 insertions, 0 deletions
diff --git a/challenge-087/stuart-little/haskell/ch-1.hs b/challenge-087/stuart-little/haskell/ch-1.hs
new file mode 100755
index 0000000000..338b91f296
--- /dev/null
+++ b/challenge-087/stuart-little/haskell/ch-1.hs
@@ -0,0 +1,15 @@
+#!/usr/bin/env runghc
+
+-- run <script> <space-separated integers>
+
+import Data.List (sort,sortOn,)
+import Data.List.Extra (groupOn,)
+import System.Environment (getArgs,)
+
+main = do
+ nrs <- getArgs >>= return.(map (read::String->Int))
+ putStrLn $ pprnt $ snd $ unzip $ last $ sortOn length $ groupOn fst $ zipWith (\x y -> (y-x,y)) [0..] $ sort nrs where
+ pprnt :: [Int] -> String
+ pprnt xs
+ |length xs <= 1 ="0"
+ |otherwise = unwords $ map show xs
diff --git a/challenge-087/stuart-little/haskell/ch-2.hs b/challenge-087/stuart-little/haskell/ch-2.hs
new file mode 100755
index 0000000000..2a47d8d607
--- /dev/null
+++ b/challenge-087/stuart-little/haskell/ch-2.hs
@@ -0,0 +1,23 @@
+#!/usr/bin/env runghc
+
+-- run <script> <space-separated 0-1 words, one per matrix row>
+
+import Data.List.Extra (maximumOn,)
+import Data.List.Utils (join,)
+import System.Environment (getArgs,)
+
+subRect :: [[a]] -> Int -> Int -> Int -> Int -> [[a]]
+subRect mat x1 x2 y1 y2 = map (\ls -> map (ls!!) [y1..y2]) $ map (mat!!) [x1..x2]
+
+isAllMat :: Eq a => a -> [[a]] -> Bool
+isAllMat c = and.(map (and.map(c==)))
+
+maxRect :: Eq a => a -> [[a]] -> Maybe [[a]]
+maxRect c mat
+ |null l = Nothing
+ |otherwise = Just $ maximumOn (\tab -> (length tab)*(length $ head tab)) l where
+ l = filter (isAllMat c) $ map (\(x1,x2,y1,y2)-> subRect mat x1 x2 y1 y2) [(x1,x2,y1,y2) | [x1,x2] <- sequence (replicate 2 [0..(length mat)-1]), [y1,y2] <- sequence (replicate 2 [0..(length $ head mat)-1]), x1 < x2, y1 < y2]
+
+main = do
+ mat <- getArgs
+ putStrLn $ maybe "0" (join "\n") $ maxRect '1' mat