aboutsummaryrefslogtreecommitdiff
path: root/challenge-064/sangeet-kar/python/ch-2.py
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-06-11 17:11:27 +0100
committerGitHub <noreply@github.com>2020-06-11 17:11:27 +0100
commit4cb5dab951b195c73af94b60a5fb1063158fdde0 (patch)
tree62aaefe7f7780e572e9675c65c0551723dcb1856 /challenge-064/sangeet-kar/python/ch-2.py
parent5992a6a617749212388658d2574500ff8861e195 (diff)
parentda076644bf4c8750a1b168ba19045d9398c6c4f2 (diff)
downloadperlweeklychallenge-club-4cb5dab951b195c73af94b60a5fb1063158fdde0.tar.gz
perlweeklychallenge-club-4cb5dab951b195c73af94b60a5fb1063158fdde0.tar.bz2
perlweeklychallenge-club-4cb5dab951b195c73af94b60a5fb1063158fdde0.zip
Merge pull request #1815 from sangeetkar/ch64
Python CH64
Diffstat (limited to 'challenge-064/sangeet-kar/python/ch-2.py')
-rwxr-xr-xchallenge-064/sangeet-kar/python/ch-2.py21
1 files changed, 21 insertions, 0 deletions
diff --git a/challenge-064/sangeet-kar/python/ch-2.py b/challenge-064/sangeet-kar/python/ch-2.py
new file mode 100755
index 0000000000..a11a70ab8b
--- /dev/null
+++ b/challenge-064/sangeet-kar/python/ch-2.py
@@ -0,0 +1,21 @@
+#!/usr/bin/env python
+
+def word_break(string, words):
+ ans = []
+ recursive_helper(string, words, [], ans)
+ return ans
+
+def recursive_helper(string, words, accum, ans):
+ if not string:
+ ans.append(accum)
+ return
+ for word in words:
+ if string.startswith(word):
+ recursive_helper(string[len(word):], words, accum + [word], ans)
+
+
+print(word_break("perlweeklychallenge", ["weekly", "challenge", "perl"]))
+print(word_break("perlandraku", ["python", "ruby", "haskell"]))
+
+# multiple solutions
+print(word_break("perlandraku", ["perl", "perland", "raku", "and"]))