diff options
| author | Myoungjin JEON <jeongoon@gmail.com> | 2020-10-02 20:00:01 +1000 |
|---|---|---|
| committer | Myoungjin JEON <jeongoon@gmail.com> | 2020-10-02 20:00:01 +1000 |
| commit | 5a4324d11f6e1005922965c46931f459f01c91d5 (patch) | |
| tree | 351bc9be1b7b960bfb3db8de04687780e1492a15 /challenge-080 | |
| parent | 7dd55476dcb0aeb0910e318587080dbad73328b5 (diff) | |
| download | perlweeklychallenge-club-5a4324d11f6e1005922965c46931f459f01c91d5.tar.gz perlweeklychallenge-club-5a4324d11f6e1005922965c46931f459f01c91d5.tar.bz2 perlweeklychallenge-club-5a4324d11f6e1005922965c46931f459f01c91d5.zip | |
[ch-080/jeongoon] Elm solution added.
Diffstat (limited to 'challenge-080')
| -rw-r--r-- | challenge-080/jeongoon/elm/elm.json | 24 | ||||
| -rw-r--r-- | challenge-080/jeongoon/elm/src/Ch1.elm | 27 | ||||
| -rw-r--r-- | challenge-080/jeongoon/elm/src/Ch2.elm | 19 | ||||
| -rw-r--r-- | challenge-080/jeongoon/elm/src/Main.elm | 113 |
4 files changed, 183 insertions, 0 deletions
diff --git a/challenge-080/jeongoon/elm/elm.json b/challenge-080/jeongoon/elm/elm.json new file mode 100644 index 0000000000..dea3450db1 --- /dev/null +++ b/challenge-080/jeongoon/elm/elm.json @@ -0,0 +1,24 @@ +{ + "type": "application", + "source-directories": [ + "src" + ], + "elm-version": "0.19.1", + "dependencies": { + "direct": { + "elm/browser": "1.0.2", + "elm/core": "1.0.5", + "elm/html": "1.0.0" + }, + "indirect": { + "elm/json": "1.1.3", + "elm/time": "1.0.0", + "elm/url": "1.0.0", + "elm/virtual-dom": "1.0.2" + } + }, + "test-dependencies": { + "direct": {}, + "indirect": {} + } +} diff --git a/challenge-080/jeongoon/elm/src/Ch1.elm b/challenge-080/jeongoon/elm/src/Ch1.elm new file mode 100644 index 0000000000..c4c138138c --- /dev/null +++ b/challenge-080/jeongoon/elm/src/Ch1.elm @@ -0,0 +1,27 @@ +module Ch1 exposing ( findSmallestMissingPN + , alternativeSmallest ) +import String + +findSmallestMissingPN : String -> ( List Int, Maybe Int ) +findSmallestMissingPN newListString = + let newNumberList = List.sort + ( List.filterMap + String.toInt (String.split " " newListString ) ) + tallest = List.maximum newNumberList + missing = case tallest of + Nothing -> [] + Just tl -> + List.filterMap + (\x -> if List.member x newNumberList then Nothing + else Just x) + (List.range 1 tl) + smallestMissing = List.head missing -- can be Just x | Nothing + in + ( newNumberList, smallestMissing ) + +-- when their is no missing smallest among the postive integers +alternativeSmallest : List Int -> Int +alternativeSmallest ls = + case List.maximum (List.filter (\x -> x > 0) ls) of + Nothing -> 1 + Just tallest -> tallest + 1 diff --git a/challenge-080/jeongoon/elm/src/Ch2.elm b/challenge-080/jeongoon/elm/src/Ch2.elm new file mode 100644 index 0000000000..8c02887840 --- /dev/null +++ b/challenge-080/jeongoon/elm/src/Ch2.elm @@ -0,0 +1,19 @@ +module Ch2 exposing (countCandies) +import String + +countCandies : String -> ( List Int, Maybe Int ) +countCandies newListString = + let newNumberList = List.sort + ( List.filterMap + String.toInt (String.split " " newListString ) ) + len = List.length newNumberList + leftGroup = if len > 0 then List.take (len-1) newNumberList else [] + rightGroup = case List.tail newNumberList of + Nothing -> [] + Just ls -> ls + candies = if len <= 1 then len + else len + ( List.sum + ( List.map2 (\l r -> + if l /= r then 1 else 0 ) + leftGroup rightGroup ) ) + in ( newNumberList, Just candies ) diff --git a/challenge-080/jeongoon/elm/src/Main.elm b/challenge-080/jeongoon/elm/src/Main.elm new file mode 100644 index 0000000000..99df8cea55 --- /dev/null +++ b/challenge-080/jeongoon/elm/src/Main.elm @@ -0,0 +1,113 @@ +{- 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 +-} + +module Main exposing (..) + +import Browser +import Html exposing (..) +import Html.Attributes exposing (..) +import Html.Events exposing (onInput) + +import String +import Tuple + +-- Solutions ... +import Ch1 +import Ch2 + +-- Main + +main = + Browser.sandbox {init = init, update = update, view = view } + +-- Model + +type alias Model = + { numberListStringT1 : String + , numberListT1 : List Int + , smallestMissingPN : Maybe Int + , numberListStringT2 : String + , numberListT2 : List Int + , candies : Maybe Int + } + +init : Model +init = Model "" [] Nothing "" [] Nothing + +-- Update + +type Task = Task1 | Task2 + +type Msg + = FindSmallestMissingPN String + | CountCandies String + +update : Msg -> Model -> Model +update msg model = + case msg of + FindSmallestMissingPN newListString -> + let res = Ch1.findSmallestMissingPN newListString + in { model + | numberListStringT1 = newListString + , numberListT1 = Tuple.first res + , smallestMissingPN = Tuple.second res } + CountCandies newListString -> + let res = Ch2.countCandies newListString + in { model + | numberListStringT2 = newListString + , numberListT2 = Tuple.first res + , candies = Tuple.second res } + +-- View + +view : Model -> Html Msg +view model = + div [ style "width" "90%" ] + [ h1 [] [ text "Task1: Smallest Positive Number"] + , viewInput "text" "Type a list of integers (negative one fine :-), sperated by space" + model.numberListStringT1 FindSmallestMissingPN + , displaySizeOfList "List Size: " model.numberListT1 + , displayAnswer Task1 "Smallest missing positive number: " + model.numberListT1 model.smallestMissingPN + , h1 [] [ text "Task2: Count Candies" ] + , viewInput "text" "Type a list of rank(numbers), seprated by space" + model.numberListStringT2 CountCandies + , displaySizeOfList "Candy Members: " model.numberListT2 + , displayAnswer Task2 "Total candies in need: " + model.numberListT2 model.candies + ] + +viewInput : String -> String -> String -> (String -> msg) -> Html msg +viewInput t p v toMsg = + div [ style "background-color" "gray" ] + [ input [ size 150, type_ t, placeholder p, value v, onInput toMsg ] [] ] + +displaySizeOfList : String -> List Int -> Html msg +displaySizeOfList sizeOfString numList = + case List.length numList of + 0 -> div [] [ text ( sizeOfString ++ "0" ) ] + x -> div [] [ text ( sizeOfString ++ (String.fromInt x) ) ] + +displayAnswer : Task -> String -> List Int -> Maybe Int -> Html msg +displayAnswer task entryString anyNumberList answer = + case answer of + Nothing -> + if List.length anyNumberList == 0 then + div [ style "color" "blue" ] [ text "List is empty" ] + else + case task of + Task1 -> + div [ style "color" "red" ] + [ text ( entryString ++ + "(not found but alternatively) " ++ + (String.fromInt(Ch1.alternativeSmallest + anyNumberList)) ) ] + _ -> + div [style "color" "red" ] + [ text "(List is not useful)" ] + Just x -> + div [ style "color" "green" ] [ text ( entryString ++ + String.fromInt(x) ) ] |
