aboutsummaryrefslogtreecommitdiff
path: root/challenge-088/jeongoon/elm/src
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/elm/src
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/elm/src')
-rw-r--r--challenge-088/jeongoon/elm/src/Ch1.elm18
-rw-r--r--challenge-088/jeongoon/elm/src/Ch2.elm79
-rw-r--r--challenge-088/jeongoon/elm/src/Main.elm129
3 files changed, 226 insertions, 0 deletions
diff --git a/challenge-088/jeongoon/elm/src/Ch1.elm b/challenge-088/jeongoon/elm/src/Ch1.elm
new file mode 100644
index 0000000000..18baf715d0
--- /dev/null
+++ b/challenge-088/jeongoon/elm/src/Ch1.elm
@@ -0,0 +1,18 @@
+module Ch1 exposing (..)
+
+import List as L
+
+errEmptyList = "Empty List"
+errSomeIsNotPostive = "Some of Element is not Postive Interger"
+
+arrayOfProduct : List Int -> Result String (List Int)
+arrayOfProduct ints =
+ let len = (L.length ints)
+ in
+ case len of
+ 0 -> Err errEmptyList
+ 1 -> Ok [1] -- not in the spec; probably correct answer
+ _ -> if (L.any (\x -> x < 1) ints) == True then
+ Err errSomeIsNotPostive
+ else let p = L.foldr (*) 1 ints
+ in Ok (L.map (\x -> p // x) ints) -- // -> int div.
diff --git a/challenge-088/jeongoon/elm/src/Ch2.elm b/challenge-088/jeongoon/elm/src/Ch2.elm
new file mode 100644
index 0000000000..1841c81750
--- /dev/null
+++ b/challenge-088/jeongoon/elm/src/Ch2.elm
@@ -0,0 +1,79 @@
+module Ch2 exposing (..)
+
+import List as L
+import String as S
+import Array as A
+import Result as R
+
+errEmptyMatrix = "Matrix is Empty"
+
+parseMatrix : String -> Result String (List (List String))
+parseMatrix str =
+ let parsed = str -- Elm's nice syntax: read from top to bottom
+ |> S.split "]"
+ |> S.join "\n"
+ |> S.lines
+ |> L.map (S.replace "[" ""
+ >> S.words >> L.filter (S.isEmpty >> not))
+ |> L.filter (L.isEmpty >> not)
+ in if L.isEmpty parsed
+ || (case L.head parsed of
+ Nothing -> True
+ Just fr -> L.isEmpty fr) then
+ Err errEmptyMatrix
+ else
+ Ok parsed
+
+getSpiralArray_ : (List (List String)) -> Int -> Int -> Int -> List String
+getSpiralArray_ mat nr nc o = -- number of rows, number of column
+ let re = o + nr - 1 -- abs index number of row end
+ ce = o + nc - 1 -- abs index number of column end
+ north = case (L.drop o >> L.head) mat of
+ Nothing -> []
+ Just rw -> (L.take (o+nc) >> L.drop o) rw
+ east = if nr <= 1 then []
+ else mat
+ |> L.drop (o+1)
+ |> L.take (nr-1)
+ |> L.filterMap (L.drop ce >> L.head)
+ south = {-if L.isEmpty east then []
+ else-}
+ if nc <= 1 then []
+ else case (L.drop re >> L.head) mat of
+ Nothing -> []
+ Just rw -> rw |> L.reverse
+ |> L.drop (o+1) |> L.take (nc-1)
+ west = {-if L.isEmpty south then []
+ else-}
+ if nr <= 2 then []
+ else mat
+ |> L.drop (o+1)
+ |> L.take (nr-2)
+ |> L.reverse
+ |> L.filterMap (L.drop o >> L.head)
+
+ nr2 = nr - 2
+ nc2 = nc - 2
+
+ in ( if re < 0 || ce < 0 then []
+ else north :: if L.isEmpty east then []
+ else east :: if L.isEmpty south then []
+ else south :: if L.isEmpty west then []
+ else west
+ :: [ getSpiralArray_
+ mat nr2 nc2 (o+1) ] )
+ |> L.concat
+
+getSpiralArray : (List (List String)) -> List String
+getSpiralArray mat =
+ let nr = L.length mat
+ nc = case L.head mat of
+ Nothing -> -1
+ Just rw -> L.length rw
+ in if nr > 0 && nc > 0 then getSpiralArray_ mat nr nc 0
+ else []
+
+getSpiralArrayFromString : String -> Result String (List String)
+getSpiralArrayFromString str =
+ str |> parseMatrix
+ |> R.map getSpiralArray
diff --git a/challenge-088/jeongoon/elm/src/Main.elm b/challenge-088/jeongoon/elm/src/Main.elm
new file mode 100644
index 0000000000..7a051aca5f
--- /dev/null
+++ b/challenge-088/jeongoon/elm/src/Main.elm
@@ -0,0 +1,129 @@
+{- Tested with:
+elm make src/Main.elm
+# and access elm/index.html in a web browser(firefox in my case)
+# which shows Task1 and Task2 altogether
+# or ...
+elm reactor
+# and access localhost:8000 (again in a web browser)
+# and click `Main.elm'
+-}
+
+module Main exposing (..)
+
+import Browser
+import Html exposing (..)
+import Html.Attributes exposing (..)
+import Html.Events exposing (onInput)
+
+import String as S
+import List as L
+import Result as R
+
+-- Solutions ...
+import Ch1 exposing (..)
+import Ch2 exposing (..)
+
+-- Main
+
+main =
+ Browser.sandbox { init = init, update = update, view = view }
+
+-- Model
+type alias Model =
+ { ch1IntListStr : String
+ , ch1Result : Result String (List Int)
+ , ch2MatrixStr : String
+ , ch2Result : Result String (List String)
+ }
+
+init : Model
+init = Model "" (Err "") "" (Err "")
+
+-- Update
+
+type Task = Task1
+type Msg = ArrayProductValueChanged String
+ | SpiralMatrixValueChanged String
+
+getPositiveIntegerListFromString : String -> Result String (List Int)
+getPositiveIntegerListFromString str =
+ let trimed_str = S.trim str
+ sls = (S.split " " trimed_str)
+ ints = L.filterMap S.toInt sls
+ in if S.length trimed_str == 0 then
+ Err "" -- indicate initial state
+ else if (L.length sls) == (L.length ints) then Ok ints
+ else Err "Some element doesn't look like a POSITIVE integer"
+
+update : Msg -> Model -> Model
+update msg m =
+ case msg of
+ ArrayProductValueChanged nv ->
+ let res = (getPositiveIntegerListFromString nv
+ |> R.andThen Ch1.arrayOfProduct)
+ in { m | ch1IntListStr = nv
+ , ch1Result = res }
+ SpiralMatrixValueChanged nv ->
+ let res = Ch2.getSpiralArrayFromString nv
+ in { m | ch2MatrixStr = nv
+ , ch2Result = res }
+
+-- View
+
+view : Model -> Html Msg
+view m =
+ div [ style "padding" "20px", style "width" "90%" ]
+ [ h1 [] [ text "Task1: Array Product" ]
+ , viewInput "text" "Positive Integers" m.ch1IntListStr
+ ArrayProductValueChanged
+ , displayAnswerCh1 m.ch1Result
+ , h1 [] [ text "Task2: Spiral Matrix" ]
+ , viewInputTextArea "Please Input a matrix data.\nex)\n[a b c]\n[d e f]\n[g h i]"
+ m.ch2MatrixStr SpiralMatrixValueChanged
+ , displayAnswerCh2 m.ch2Result
+ ]
+
+viewInput : String -> String -> String -> (String -> msg) -> Html msg
+viewInput t p v toMsg =
+ div [ style "dispay" "inline-block" ]
+ [ input [ size 150, type_ t, placeholder p, value v, onInput toMsg ] []]
+
+viewInputTextArea : String -> String -> (String -> msg) -> Html msg
+viewInputTextArea p v toMsg =
+ div [ style "display" "inline-block" ]
+ [ textarea [ rows 5, cols 150
+ , placeholder p, value v, onInput toMsg ] [] ]
+
+displayAnswerCh1 : Result String (List Int) -> Html msg
+displayAnswerCh1 result =
+ displayAnswer "Result"
+ (case result of
+ Ok ints -> Ok ("(" ++ (S.join ","
+ (L.map S.fromInt ints)) ++")")
+ Err e -> Err e)
+
+displayAnswerCh2 : Result String (List String) -> Html msg
+displayAnswerCh2 result =
+ displayAnswer "Result"
+ (case result of
+ Ok words -> Ok ("(" ++ (S.join "," words) ++ ")")
+ Err e -> Err e)
+
+
+displayAnswer : String -> Result String String -> Html msg
+displayAnswer entryString result =
+ case result of
+ Ok msg -> div []
+ [ viewReadOnlyTextArea "green" msg ]
+ Err e ->
+ if e == ""
+ then div []
+ [ viewReadOnlyTextArea "blue"
+ (entryString ++ ": please input all the entries") ]
+ else div []
+ [ viewReadOnlyTextArea "red" e ]
+
+viewReadOnlyTextArea : String -> String -> Html msg
+viewReadOnlyTextArea color str =
+ textarea [ readonly True, style "color" color
+ , rows 15, cols 150, value str ] []