-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathall_construct.py
37 lines (35 loc) · 1.15 KB
/
all_construct.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
def all_constructs(target, word_bank, memo=None):
if len(target) == 0:
return [[]] # Solution reached
if memo is None:
memo = {}
elif target in memo.keys():
return memo[target]
allconstruncts = []
for w in word_bank:
if target.startswith(w):
new_target = target.removeprefix(w)
construncts = all_constructs(new_target, word_bank, memo)
# Alternative with list - map:
# allconstruncts.extend(list(map(lambda c: [w] + c, construncts)))
for construnct in construncts:
construnct = [w] + construnct
allconstruncts.append(construnct)
memo[target] = allconstruncts
return allconstruncts
tests = [
('purple', ['purp', 'p', 'ur', 'le', 'purpl']),
('hello', ['cat', 'dog', 'cat', 'mouse']),
('', ['cat', 'dog', 'cat', 'mouse']),
('abcdef', ['ab', 'abc', 'cd', 'def', 'abcd', 'ef', 'c']),
('eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeef', [
'e',
'eee',
'eeee',
'eeeeee',
'eeeeeeeee',
'eeeeeeeeeeeee'
])
]
for test in tests:
print(all_constructs(test[0], test[1]))