From 14139a86f2f94a42393d4b715fec91ebb56437bc Mon Sep 17 00:00:00 2001 From: Myoungjin JEON Date: Thu, 15 Oct 2020 13:36:09 +1100 Subject: [ch-082/jeongoon] a blog added. --- challenge-082/jeongoon/blog.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 challenge-082/jeongoon/blog.txt diff --git a/challenge-082/jeongoon/blog.txt b/challenge-082/jeongoon/blog.txt new file mode 100644 index 0000000000..9c6220659a --- /dev/null +++ b/challenge-082/jeongoon/blog.txt @@ -0,0 +1 @@ +https://dev.to/jeongoon/weekly-challenge-082-task-1-raku-gg1 -- cgit From e82c1cad04bc5202cdda9f36ccaad78c47cffe98 Mon Sep 17 00:00:00 2001 From: Myoungjin JEON Date: Thu, 15 Oct 2020 16:18:49 +1100 Subject: [ch-082/jeongoon] Ch2.elm modified ch-2.hs added --- challenge-082/jeongoon/elm/src/Ch2.elm | 2 +- challenge-082/jeongoon/haskell/ch-2.hs | 101 +++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 challenge-082/jeongoon/haskell/ch-2.hs diff --git a/challenge-082/jeongoon/elm/src/Ch2.elm b/challenge-082/jeongoon/elm/src/Ch2.elm index fd4a61a14b..85d1cb2de3 100644 --- a/challenge-082/jeongoon/elm/src/Ch2.elm +++ b/challenge-082/jeongoon/elm/src/Ch2.elm @@ -69,7 +69,7 @@ sps cn origSum isValid parts siblings = -- cn: current number moreParts = {-same as-}testParts nextNumber = cn + 1 lowerCases = sps 1 restSum - isValid moreParts siblings in + isValid moreParts [] in if isValid testParts then if cn == origSum then -- reach the max: no more next cases diff --git a/challenge-082/jeongoon/haskell/ch-2.hs b/challenge-082/jeongoon/haskell/ch-2.hs new file mode 100644 index 0000000000..a3fc204f5a --- /dev/null +++ b/challenge-082/jeongoon/haskell/ch-2.hs @@ -0,0 +1,101 @@ +import System.Environment +import System.Exit +import Data.Maybe (catMaybes) +import Data.List (intersect, intercalate) + +data WhichPart = Odd | Even deriving (Show, Eq) + +decomposeAsEachOddsEvensWith :: String -> [Int] -> ([String], String, String) +decomposeAsEachOddsEvensWith origStr splitRules = +-- note: this is unsafe method + decompose [] "" "" origStr splitRules Odd + where + decompose ls os es _ [] _ = (ls, os, es) + decompose ls os es str (p:ps) wp = + case wp of + Odd -> decompose ls' os' es str' ps Even + Even -> decompose ls' os es' str' ps Odd + where + cs = take p str; str' = drop p str + os' = (os ++ cs); es' = (es ++ cs); ls' = (ls ++ [cs]) + +-- entry point +allInterleavedCases :: String -> String -> String + -> Either String [([String], String, String)] +allInterleavedCases sa sb sc + | (length sa) + (length sb) /= targetSum + = Left "length of strings are not matched." + | null confirmedCases = Left "not interleaved" + | otherwise = Right confirmedCases + + where + targetSum = length sc + confirmedCases = + catMaybes $ maybe [] (map (\ils -> + if (length ils) == 1 then Nothing + else checkCaseFully ils ) ) possilbeCases + + possilbeCases = sps 1 targetSum maybeOk [] [] + + checkCaseFully :: [Int] -> Maybe ([String], String, String) + checkCaseFully ps = + let (cs, o, e) = sc `decomposeAsEachOddsEvensWith` ps + in + if (sa == o && sb == e) + || (sb == o && sa == e) then Just (cs, o, e) + else + Nothing + + maybeOk :: [Int] -> Bool + maybeOk rule + | length rule <= 1 = True -- skip as okay, note: we have to sieve later + | otherwise = + + let (cs, a, b) = sc `decomposeAsEachOddsEvensWith` rule + in + any (\(sa', sb') -> + any ((intersect a sa')==) [a,sa'] + && any ((intersect b sb')==) [b,sb']) [(sa,sb),(sb,sa)] + +-- sps: some possible sum (permutations with repeatition with filter) +sps :: Int -> Int -> {-filter <= -}([Int] -> Bool) + -> [Int] -> [[Int]] -> Maybe [[Int]] +sps cn origSum isValid parts siblings + | origSum == 0 = Just [] + | (not.isValid) testParts = if null siblings then Nothing else Just siblings + | cn == origSum = + -- reach the max case in current depth: no more next cases + -- put siblings and this one together + Just (siblings ++ [[cn]]) + | otherwise = -- try next cases with saving current cases if any + sps nextNumber origSum isValid parts us + + where + parts' = parts ++ [cn] + testParts = parts' + restSum = origSum - cn + nextNumber = succ cn + us = case currentCases of + Nothing -> siblings + Just cases -> siblings ++ cases + + currentCases = fmap (map (cn:)) lowerCases + lowerCases = sps 1 restSum isValid parts' [] + +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 " + else + case allInterleavedCases sa sb sc of + Left err -> putStrLn $ "0 as " ++ err + Right cases -> do + putStrLn $ "1 as we found " ++ ((show.length) cases) + ++ " possible cases found.\n" + ++ "in other words, \"" ++ sc ++ "\" can be decomposed as below" + mapM_ (\(cs, a, b) -> do + putStrLn $ "[" ++ + ((intercalate ", ".map (\s -> "\""++ s ++"\"")) cs) ++ "]" + ++ " --> " ++ a ++ ", " ++ b + ) cases -- cgit