aboutsummaryrefslogtreecommitdiff
path: root/challenge-088/jeongoon/haskell
diff options
context:
space:
mode:
authorMyoungjin JEON <jeongoon@gmail.com>2020-11-29 22:46:46 +1100
committerMyoungjin JEON <jeongoon@gmail.com>2020-11-29 22:46:46 +1100
commit8178fe5e04e95cfbc20c8bb2d4210bc848cb76af (patch)
treeb087467fa501c5576ed7328368a52dfba37b7be8 /challenge-088/jeongoon/haskell
parent71c1105173866e6b2419a40255465ef74a1fc280 (diff)
downloadperlweeklychallenge-club-8178fe5e04e95cfbc20c8bb2d4210bc848cb76af.tar.gz
perlweeklychallenge-club-8178fe5e04e95cfbc20c8bb2d4210bc848cb76af.tar.bz2
perlweeklychallenge-club-8178fe5e04e95cfbc20c8bb2d4210bc848cb76af.zip
[ch-088/jeongoon] Perl, Raku, Haskell, Go, Common-lisp, Elm Solution added
Diffstat (limited to 'challenge-088/jeongoon/haskell')
-rw-r--r--challenge-088/jeongoon/haskell/ch-1.hs13
-rw-r--r--challenge-088/jeongoon/haskell/ch-2.hs81
2 files changed, 94 insertions, 0 deletions
diff --git a/challenge-088/jeongoon/haskell/ch-1.hs b/challenge-088/jeongoon/haskell/ch-1.hs
new file mode 100644
index 0000000000..d3d52dce8a
--- /dev/null
+++ b/challenge-088/jeongoon/haskell/ch-1.hs
@@ -0,0 +1,13 @@
+import System.Environment
+import System.Exit
+import Data.Char (isNumber)
+import Data.Maybe (catMaybes)
+
+arrayOfProduct [] = [1] -- assume 1 as spec doesn't consider this case
+arrayOfProduct ns = let total = product ns in map (total `div`) ns
+
+main = do
+ ns <- (catMaybes.map (\nStr ->
+ if (all isNumber nStr) then Just(read nStr :: Int)
+ else Nothing )) `fmap` getArgs;
+ print $ arrayOfProduct ns
diff --git a/challenge-088/jeongoon/haskell/ch-2.hs b/challenge-088/jeongoon/haskell/ch-2.hs
new file mode 100644
index 0000000000..691aaad582
--- /dev/null
+++ b/challenge-088/jeongoon/haskell/ch-2.hs
@@ -0,0 +1,81 @@
+import Data.List (groupBy, transpose, unfoldr)
+import Data.Maybe (catMaybes)
+
+{- tested with:
+echo "[a b c d][e f g h][i j k l]" | runhaskell ch-2.hs
+echo "[1 2 3][4 5 6][7 8 9]" | runhaskell ch-2.hs
+echo "[a b][c d][e f]" | runhaskell ch-2.hs
+-}
+
+getMatrixFromStdin :: IO [[String]]
+getMatrixFromStdin =
+ ( map words
+ . filter ((0/=).length) -- 5. (after 3) there is some empty row
+ . map (filter (`notElem` "[]")) -- 4. '[', ']' is not used
+ . lines -- 3. devide it into rows
+ . unlines -- 2. make it "]\n"
+ . groupBy (\_ b -> b/= ']') ) -- 1. devide row by "]"
+ `fmap` getContents
+
+-- cut outside of a matrix
+getInnerMatrix :: [[String]] -> Either String [[String]]
+getInnerMatrix m
+ | length m <= 1 = Left "Too Short In Row"
+ | otherwise = mapCutColumns [] getShorterInRows
+ where
+ mapCutColumns acc [] = Right acc
+ mapCutColumns acc (r:rs) =
+ case getShorterInCols r of
+ Nothing -> Left "Too Short In Column"
+ Just r' -> mapCutColumns (acc ++ [r']) rs
+ getShorterInRows = (init.tail) m -- cut top and bottom
+ getShorterInCols row =
+ if (length row) <= 1 then Nothing
+ else Just $ (init.tail) row
+
+-- read outside cells starting from (0,0) in clockwise
+readAroundMatrixCW :: [[String]] -> [String]
+readAroundMatrixCW [] = []
+readAroundMatrixCW m =
+ foldr1 (++) $
+ getNorth
+ : case findEast of
+ Nothing -> []
+ Just e -> e
+ : case findSouth of
+ Nothing -> []
+ Just s -> s
+ : case findWest of
+ Nothing -> []
+ Just w -> [w]
+ where
+ tm = transpose m
+ getNorth = head m
+ findEast = if (length m) == 1 then Nothing
+ else Just $ (tail.last) tm
+ findSouth = {-if (length m) == 1 then Nothing
+ else-} -- already checked when findEast
+ let lm = (last m)
+ in if (length lm) == 1 then Nothing
+ else Just $ (reverse.init) lm
+ findWest = {-if (length m) == 1 then Nothing
+ else-}
+ let htm = (head tm)
+ in if (length htm) <= 2 then Nothing
+ else Just $ (reverse.tail.init) htm
+
+getSpiralAarray :: [[String]] -> [String]
+getSpiralAarray mat =
+ foldr1 (++)
+ ( unfoldr (\m ->
+ case m of
+ Nothing -> Nothing
+ Just m' -> case (getInnerMatrix m') of
+ Left _ -> Just (outOf m', Nothing)
+ Right m'' -> Just (outOf m', Just m''))
+ (Just mat) )
+ where outOf m = readAroundMatrixCW m
+
+main = do
+ getSpiralAarray `fmap` getMatrixFromStdin
+ >>= print