diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-10-03 13:54:54 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-10-03 13:54:54 +0100 |
| commit | df86e6274f452ee9b08f6e57be66f94459d83475 (patch) | |
| tree | 0f33c653a9b4e1fc3754be8c6f8d132241447e53 | |
| parent | 94604731f2e92e147fec703a1a732158f5a830ea (diff) | |
| parent | 2f882cfb91c2af0320e74cd678f4a102c85f0d0f (diff) | |
| download | perlweeklychallenge-club-df86e6274f452ee9b08f6e57be66f94459d83475.tar.gz perlweeklychallenge-club-df86e6274f452ee9b08f6e57be66f94459d83475.tar.bz2 perlweeklychallenge-club-df86e6274f452ee9b08f6e57be66f94459d83475.zip | |
Merge pull request #2434 from jeongoon/master
[ch-080/jeongoon] Elm, Go solution added
| -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 | ||||
| -rw-r--r-- | challenge-080/jeongoon/go/ch-1.go | 74 | ||||
| -rw-r--r-- | challenge-080/jeongoon/go/ch-2.go | 48 |
6 files changed, 305 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) ) ] diff --git a/challenge-080/jeongoon/go/ch-1.go b/challenge-080/jeongoon/go/ch-1.go new file mode 100644 index 0000000000..d78b813325 --- /dev/null +++ b/challenge-080/jeongoon/go/ch-1.go @@ -0,0 +1,74 @@ +package main + +/* Ref: +https://tour.golang.org/flowcontrol/1 +https://www.davidkaya.com/sets-in-golang/ +https://golangdocs.com/converting-string-to-integer-in-golang +*/ + +/* test with: +go run ch-1.go -2 3 5 2 1 +# > 4 +*/ + +import ( + "fmt" + "os" + "strconv" +) + +var exists = struct{}{} + +type intSet struct { + m map[int]struct{} +} + +func NewIntSet() *intSet { + s := &intSet{} + s.m = make(map[int]struct{}) + return s +} + +func (s *intSet) Add(value int) { + s.m[value] = exists +} + +func (s *intSet) Remove(value int) { + delete(s.m, value) +} + +func (s *intSet) Contains(value int) bool { + _, c := s.m[value] + return c +} + +func main() { + set := NewIntSet() + max := 0 + args := os.Args[1:] // ignore programme name + + for _, it := range args { + int_val, err := strconv.Atoi( it ) + if err != nil { + fmt.Fprint( os.Stderr, err ) + fmt.Fprintln( os.Stderr, ": skipped" ) + } + if int_val < 0 { + fmt.Fprintln( os.Stderr, + "negative values are unnecessary: skipped" ) + } else { + set.Add( int_val ) + if max < int_val { + max = int_val + } + } + } + max++; + + for maybe_missing := 1; maybe_missing <= max; maybe_missing++ { + if ! set.Contains( maybe_missing ) { + fmt.Println( maybe_missing ) + break + } + } +} diff --git a/challenge-080/jeongoon/go/ch-2.go b/challenge-080/jeongoon/go/ch-2.go new file mode 100644 index 0000000000..3a78e3fe1c --- /dev/null +++ b/challenge-080/jeongoon/go/ch-2.go @@ -0,0 +1,48 @@ +package main + +/* Ref: +https://golangdocs.com/slices-in-golang +*/ + +/* test with: +go run ch-2.go 1 5 4 2 +# > 6 +*/ + +import ( + "fmt" + "os" + "strconv" +) + +func main() { + args := os.Args[1:] // ignore programme name + rank := []int{} + + for _, it := range args { + int_val, err := strconv.Atoi( it ) + if err != nil { + fmt.Fprint( os.Stderr, err ) + fmt.Fprintln( os.Stderr, ": skipped" ) + continue + } + rank = append( rank, int_val ) + } + rank_len := len(rank) + if rank_len == 0 { + fmt.Println("0 as no useuful data") + os.Exit(1) + } + + left := rank[0:rank_len -1] + right := rank[1:] + + candies := rank_len + + for i := 0; i < rank_len -2; i++ { + if left[i] != right[i] { + candies++ + } + } + fmt.Println(candies) +} |
