diff options
| -rw-r--r-- | challenge-082/jeongoon/elm/src/Ch1.elm | 39 | ||||
| -rw-r--r-- | challenge-082/jeongoon/elm/src/Ch2.elm | 5 | ||||
| -rw-r--r-- | challenge-082/jeongoon/elm/src/Main.elm | 71 | ||||
| -rw-r--r-- | challenge-082/jeongoon/haskell/ch-1.hs | 23 | ||||
| -rw-r--r-- | challenge-082/jeongoon/haskell/ch-2.hs | 6 |
5 files changed, 126 insertions, 18 deletions
diff --git a/challenge-082/jeongoon/elm/src/Ch1.elm b/challenge-082/jeongoon/elm/src/Ch1.elm new file mode 100644 index 0000000000..84b6dfdf0f --- /dev/null +++ b/challenge-082/jeongoon/elm/src/Ch1.elm @@ -0,0 +1,39 @@ +module Ch1 exposing ( getCommonFactors ) + +import String as S +import List as L +import Maybe as Mb + +getCommonFactors : Int -> Int -> Result String (List Int) +getCommonFactors m n = + if L.all (\x -> x < 0 ) [m, n] + then Err "" -- probably initial state or equivalent to. + else if L.any(\x -> x < 1) [m, n] + then Err "Please input all the strings !!!" + else + case commonFactors [m, n] of + [] -> Err "invalid input" + ls -> Ok ls + +commonFactors : List Int -> List Int +commonFactors ils = + case ils of + [] -> [] + ls -> + let l = L.minimum ls + g = L.maximum ls in + case Mb.map2 (\a b -> a == b) l g of + Nothing -> [] + Just same -> + case l of + Nothing -> [] -- impossible but have to + Just jl -> + if same then + -- note: modBy has opposite order of arguments + -- when compared to most of other language + L.filter (\dv -> modBy dv jl ==0) (L.range 1 jl) + else + L.filter (\dv -> + L.all (\i -> + modBy dv i == 0) ls) + (L.range 1 jl) diff --git a/challenge-082/jeongoon/elm/src/Ch2.elm b/challenge-082/jeongoon/elm/src/Ch2.elm index 85d1cb2de3..5d2f7d7422 100644 --- a/challenge-082/jeongoon/elm/src/Ch2.elm +++ b/challenge-082/jeongoon/elm/src/Ch2.elm @@ -1,7 +1,6 @@ -module Ch2 exposing ( .. {- - checkInterleavedString +module Ch2 exposing ( checkInterleavedString , allPossiblePartitions - , WhichPart-} ) + , WhichPart ) import String as S import List as L diff --git a/challenge-082/jeongoon/elm/src/Main.elm b/challenge-082/jeongoon/elm/src/Main.elm index 6d1863fcb1..a128fc6e08 100644 --- a/challenge-082/jeongoon/elm/src/Main.elm +++ b/challenge-082/jeongoon/elm/src/Main.elm @@ -1,6 +1,7 @@ {- 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 (..) @@ -14,6 +15,7 @@ import String as S import List as L -- Solutions ... +import Ch1 exposing (..) import Ch2 exposing (..) -- Main @@ -24,28 +26,44 @@ main = -- Model type alias Model = - { ch2StringA : String + { ch1IntM : Int + , ch1IntN : Int + , ch1Result : Result String (List Int) + , ch2StringA : String , ch2StringB : String , ch2StringC : String , ch2Result : Result String (List (List String)) } init : Model -init = Model "" "" "" (Err "") +init = Model 0 0 (Err "") "" "" "" (Err "") -- Update type Task = Task1 | Task2 -type Msg = {-CommonFactors String | -} - InterleavedStringFirst String +type Msg = CommonFactorsM String + | CommonFactorsN String + | InterleavedStringFirst String | InterleavedStringSecond String | InterleavedStringInterleaved String update : Msg -> Model -> Model update msg m = - case msg of - InterleavedStringFirst nv -> -- new value + case msg of -- nv: new value + CommonFactorsM nv -> + case S.toInt nv of + Nothing -> { m | ch1IntM = 0 } + Just ni -> + let res = Ch1.getCommonFactors ni m.ch1IntN + in { m | ch1IntM = ni, ch1Result = res } + CommonFactorsN nv -> + case S.toInt nv of + Nothing -> { m | ch1IntN = 0 } + Just ni -> + let res = Ch1.getCommonFactors m.ch1IntM ni + in { m | ch1IntN = ni, ch1Result = res } + InterleavedStringFirst nv -> let res = Ch2.checkInterleavedString nv m.ch2StringB m.ch2StringC in { m | ch2StringA = nv, ch2Result = res } InterleavedStringSecond nv -> @@ -59,7 +77,11 @@ update msg m = view : Model -> Html Msg view m = div [ style "padding" "20px", style "width" "90%" ] - [ h1 [] [ text "Task2: Interleave String" ] + [ h1 [] [ text "Task1: Common Factors" ] + , viewInputInt "text" "$M" m.ch1IntM CommonFactorsM + , viewInputInt "text" "$N" m.ch1IntN CommonFactorsN + , displayAnswerCh1 m.ch1Result + , h1 [] [ text "Task2: Interleave String" ] , viewInput "text" "String A" m.ch2StringA InterleavedStringFirst , viewInput "text" "String B" @@ -69,31 +91,52 @@ view m = , displayAnswerCh2 m.ch2StringC m.ch2Result ] +viewInputInt : String -> String -> Int -> (String -> msg) -> Html msg +viewInputInt t p i toMsg = + viewInput t p (if i < 0 then "" + else S.fromInt i ) toMsg + viewInput : String -> String -> String -> (String -> msg) -> Html msg viewInput t p v toMsg = div [ style "display" "inline-block" ] [ input [ size 150, type_ t, placeholder p, value v, onInput toMsg ] []] +displayAnswerCh1 : Result String (List Int) -> Html msg +displayAnswerCh1 result = + displayAnswer "Result" + (case result of + Ok ints -> + Ok ("(" ++ (S.join "," + (L.map S.fromInt ints)) ++ ")") + Err e -> Err e) + displayAnswerCh2 : String -> Result String (List (List String)) -> Html msg displayAnswerCh2 stringC result = displayAnswer "Result: " (case result of Ok listOfStringList -> - Ok ("Interleaved as `" ++ stringC ++ "' can be divided into\n" - ++ (S.join " or " + Ok ("1 (Interleaved) as `" ++ stringC ++ "' can be divided into\n" + ++ (S.join "\n" (L.map (\sl -> "[" ++ (S.join ", " sl) ++ "]" ) listOfStringList ))) Err e -> Err e) +viewReadOnlyTextArea : String -> String -> Html msg +viewReadOnlyTextArea color str = + textarea [ readonly True, style "color" color + , rows 15, cols 150, value str ] [] + displayAnswer : String -> Result String String -> Html msg displayAnswer entryString result = case result of - Ok str -> div [ style "color" "green" ] - [ text (entryString ++ str) ] + Ok msg -> div [] + [ viewReadOnlyTextArea "green" msg ] Err e -> if e == "" - then div [ style "color" "blue" ] - [ text (entryString ++ "please input all the entries") ] - else div [ style "color" "red" ] [ text e ] + then div [] + [ viewReadOnlyTextArea "blue" + (entryString ++ "please input all the entries") ] + else div [] + [ viewReadOnlyTextArea "red" e ] diff --git a/challenge-082/jeongoon/haskell/ch-1.hs b/challenge-082/jeongoon/haskell/ch-1.hs new file mode 100644 index 0000000000..ccfb0ce0e9 --- /dev/null +++ b/challenge-082/jeongoon/haskell/ch-1.hs @@ -0,0 +1,23 @@ +import System.Environment +import System.Exit +import Data.Char (isNumber) +import Data.Maybe (isJust, catMaybes) + +{- test with: +runhaskell ch-1.hs 12 18 +-} + +-- from ch-081/ch-1.hs :-] +--commonDivisors :: (Integral a) => [a] -> [a] +commonDivisors [] = [] +commonDivisors xs = filter (\cd -> all ((==0).(flip rem) cd) xs) [1..(gcd')] + where gcd' = foldr1 (\acc x -> gcd acc x) xs + +main = do + (catMaybes.map (\nStr -> + if (all isNumber nStr) then Just(read nStr :: Int) + else Nothing )) `fmap` getArgs + >>= (\nums -> + if length nums /= 2 then + die "Usage: runhaskell ch-1.hs <natural num> <natural num>" + else (putStrLn.show.commonDivisors.take 2) nums ) diff --git a/challenge-082/jeongoon/haskell/ch-2.hs b/challenge-082/jeongoon/haskell/ch-2.hs index a3fc204f5a..5dd2eff3fd 100644 --- a/challenge-082/jeongoon/haskell/ch-2.hs +++ b/challenge-082/jeongoon/haskell/ch-2.hs @@ -3,6 +3,10 @@ import System.Exit import Data.Maybe (catMaybes) import Data.List (intersect, intercalate) +{- test with: +runhaskell ch-2.hs XXY XXZ XXXXZY +-} + data WhichPart = Odd | Even deriving (Show, Eq) decomposeAsEachOddsEvensWith :: String -> [Int] -> ([String], String, String) @@ -86,7 +90,7 @@ main = do args <- getArgs; let sa = args !! 0; sb = args !! 1; sc = args !! 2 in if length args /= 3 - then die "Usage: runhaskell ch-1.hs <string A> <string B> <string C>" + then die "Usage: runhaskell ch-2.hs <string A> <string B> <string C>" else case allInterleavedCases sa sb sc of Left err -> putStrLn $ "0 as " ++ err |
