diff options
| author | Myoungjin JEON <jeongoon@gmail.com> | 2020-08-23 23:00:21 +1000 |
|---|---|---|
| committer | Myoungjin JEON <jeongoon@gmail.com> | 2020-08-23 23:00:21 +1000 |
| commit | 65ab3f0cc83999978ca82e3b3972d3f4108e48fc (patch) | |
| tree | 5d16b6f02713f90751d0ef532e241018e7c8f985 | |
| parent | 318fbebb0f2bcc91c635ecf282be985dcf1cd87c (diff) | |
| download | perlweeklychallenge-club-65ab3f0cc83999978ca82e3b3972d3f4108e48fc.tar.gz perlweeklychallenge-club-65ab3f0cc83999978ca82e3b3972d3f4108e48fc.tar.bz2 perlweeklychallenge-club-65ab3f0cc83999978ca82e3b3972d3f4108e48fc.zip | |
[ch-074/jeongoon] add Elm Solution
| -rw-r--r-- | challenge-074/jeongoon/elm/elm.json | 24 | ||||
| -rw-r--r-- | challenge-074/jeongoon/elm/src/Ch1.elm | 22 | ||||
| -rw-r--r-- | challenge-074/jeongoon/elm/src/Ch2.elm | 38 | ||||
| -rw-r--r-- | challenge-074/jeongoon/elm/src/Main.elm | 106 |
4 files changed, 190 insertions, 0 deletions
diff --git a/challenge-074/jeongoon/elm/elm.json b/challenge-074/jeongoon/elm/elm.json new file mode 100644 index 0000000000..dea3450db1 --- /dev/null +++ b/challenge-074/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-074/jeongoon/elm/src/Ch1.elm b/challenge-074/jeongoon/elm/src/Ch1.elm new file mode 100644 index 0000000000..b66343e505 --- /dev/null +++ b/challenge-074/jeongoon/elm/src/Ch1.elm @@ -0,0 +1,22 @@ +module Ch1 exposing (pairNumberListAndMajor) +import String +import Tuple +import List exposing (map, head, tail) + +pairNumberListAndMajor : String -> ( List Int, Maybe Int ) +pairNumberListAndMajor newListString = + let newNumberList = List.filterMap + String.toInt (String.split " " newListString) + count k = List.length( List.filter (\y -> k == y) newNumberList ) + halfLen = List.length( newNumberList ) // 2 -- //-> integer division + findMajor maybels = + case maybels of + Nothing -> Nothing + Just ls -> + case (List.head ls) of + Nothing -> Nothing + Just m -> if (count m) > halfLen + then Just m + else findMajor (List.tail ls) + in + ( newNumberList, findMajor (Just newNumberList) ) diff --git a/challenge-074/jeongoon/elm/src/Ch2.elm b/challenge-074/jeongoon/elm/src/Ch2.elm new file mode 100644 index 0000000000..4a0f1c3768 --- /dev/null +++ b/challenge-074/jeongoon/elm/src/Ch2.elm @@ -0,0 +1,38 @@ +module Ch2 exposing (getFNR) +import String exposing (join, split) +import Dict +import List exposing (head, tail) +import Maybe exposing (withDefault) + +getFNR : String -> Maybe String +getFNR newString = + let chrs = split "" newString + counts x dic = case (Dict.get x dic) of + Nothing -> 0 + Just n -> n + doFind dic rfnr cs = + case (head cs) of + Nothing -> + if List.length rfnr == 0 then Nothing + else Just (join "" (List.reverse rfnr)) + Just k -> + case (counts k dic) of + 0 -> doFind (Dict.insert k 1 dic) (k :: rfnr) + (withDefault [] (tail cs)) + m -> doFind (Dict.insert k (m+1) dic) + ((findPrevious + (Dict.insert k (m+1) dic) rfnr) + :: rfnr) + (withDefault [] (tail cs)) + findPrevious dic nrs = + case (head nrs) of + Nothing -> "#" + Just y -> + case (Dict.get y dic) of + Nothing -> y + Just u -> if u == 1 then y + else findPrevious dic + (withDefault [] (tail nrs)) + + in + doFind Dict.empty [] chrs diff --git a/challenge-074/jeongoon/elm/src/Main.elm b/challenge-074/jeongoon/elm/src/Main.elm new file mode 100644 index 0000000000..936a00d7a4 --- /dev/null +++ b/challenge-074/jeongoon/elm/src/Main.elm @@ -0,0 +1,106 @@ +{- 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: couldn't find the better way +-} + +module Main exposing (..) +import Browser +import Html exposing (..) +import Html.Attributes exposing (..) +import Html.Events exposing (onInput) + +import String +import List exposing (map, head, tail) + +import Ch1 +import Ch2 + +-- MAIN + +main = + Browser.sandbox { init = init, update = update, view = view } + +-- MODEL + +type alias Model = + { numberListString : String + , numberList : List Int + , majorNumber : Maybe Int + , charListString : String + , fnrString : Maybe String + } + +init : Model +init = + Model "" [] Nothing "" Nothing + +-- UPDATE + +type Msg + = FindMajor String + | GetFNR String + +update : Msg -> Model -> Model +update msg model = + + case msg of + FindMajor newListString -> + let result = Ch1.pairNumberListAndMajor newListString + in { model + | numberListString = newListString, + numberList = Tuple.first result, + majorNumber = Tuple.second result } + GetFNR newString -> + { model + | charListString = newString, + fnrString = if String.length newString == 0 then Nothing + else Ch2.getFNR newString } + + +-- VIEW + +view : Model -> Html Msg +view model = + div [ style "width" "90%" ] + [ h1 [] [ text "Task1: Majority Element" ] + , viewInput "text" "Type List of numbers, separated by space" + model.numberListString FindMajor + , displaySizeOfList model + , displayMajor model + , h1 [] [ text "Task2: FNR" ] + , viewInput "text" "Type a string consists of characters" + model.charListString GetFNR + , displayFNR model + ] + + +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 : Model -> Html msg +displaySizeOfList model = + case (List.length model.numberList) of + 0 -> div [] [ text "Size: 0" ] + x -> div [] [ text ( "Size: " ++ (String.fromInt x) ) ] + +displayMajor : Model -> Html msg +displayMajor model = + case model.majorNumber of + Nothing -> + if List.length model.numberList == 0 then + div [ style "color" "blue" ] [ text "List is empty" ] + else + div [ style "color" "red" ] [ text "Major Not Found: -1" ] + Just x -> + div [ style "color" "green" ] [ text ( "Major Number: " ++ + String.fromInt(x) ) ] + +displayFNR model = + case model.fnrString of + Nothing -> + div [ style "color" "red" ] [ text "No String given" ] + Just s -> + div [ style "color" "green" ] [ text s ] |
