aboutsummaryrefslogtreecommitdiff
path: root/challenge-074/jeongoon/elm/src/Main.elm
blob: 936a00d7a4ff5654b618fc609744ee8e83a73019 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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 ]