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
107
108
109
110
111
112
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) ) ]
|