aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-10-15 07:50:00 +0100
committerGitHub <noreply@github.com>2020-10-15 07:50:00 +0100
commit1774bf623614b0a632ef11d01e4fedd1bd427bd5 (patch)
treefd82ecf6ebb851284403a61e9f1503284e975a2b
parentfbd351f17d2386b990425b937ecbb8d6bd93ef9a (diff)
parente82c1cad04bc5202cdda9f36ccaad78c47cffe98 (diff)
downloadperlweeklychallenge-club-1774bf623614b0a632ef11d01e4fedd1bd427bd5.tar.gz
perlweeklychallenge-club-1774bf623614b0a632ef11d01e4fedd1bd427bd5.tar.bz2
perlweeklychallenge-club-1774bf623614b0a632ef11d01e4fedd1bd427bd5.zip
Merge pull request #2530 from jeongoon/master
[ch-082/jeongoon] a blog added.
-rw-r--r--challenge-082/jeongoon/blog.txt1
-rw-r--r--challenge-082/jeongoon/elm/src/Ch2.elm2
-rw-r--r--challenge-082/jeongoon/haskell/ch-2.hs101
3 files changed, 103 insertions, 1 deletions
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
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 <string A> <string B> <string C>"
+ 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