-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8-all_construct.py
More file actions
62 lines (50 loc) · 1.97 KB
/
Copy path8-all_construct.py
File metadata and controls
62 lines (50 loc) · 1.97 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import functools
print("******* Recursive and Memoization *******")
def list_to_tuple(function):
def wrapper(*args):
args = [tuple(x) if type(x) == list else x for x in args]
result = function(*args)
result = tuple(result) if type(result) == list else result
return result
return wrapper
@list_to_tuple
@functools.lru_cache(maxsize=None)
def all_construct(target, word_bank):
if target == "": return [[]]
r = []
for word in word_bank:
if target.startswith(word):
suffix = target[len(word):]
suffix_ways = all_construct(suffix, word_bank)
target_ways = [el[:] for el in suffix_ways]
[el.insert(0, word) for el in target_ways]
r.extend(target_ways)
return r
print(all_construct('hello', ['cat', 'dog', 'mouse']))
# []
print(all_construct('', ['cat', 'dog', 'mouse']))
# [[]]
print(all_construct('purple', ['purp', 'p', 'ur', 'le', 'purpl']))
# [['le', 'purp'], ['le', 'p', 'ur', 'p']]
print(all_construct('abcdef', ['ab', 'abc', 'cd', 'def', 'abcd', 'ef', 'c']))
# [['ef', 'cd', 'ab'], ['def', 'c', 'ab'], ['def', 'abc'], ['ef', 'abcd']]
print("******* Tabulation *******")
def all_construct2(target, word_bank):
table = [[] for i in range(len(target)+1)]
table[0] = [[]]
for i in range(len(target)):
for word in word_bank:
if word == target[i:(i+len(word))]:
for e in table[i]:
temp = e[:]
temp.append(word)
table[i+len(word)].append(temp[:])
return table[len(target)]
print(all_construct2('hello', ['cat', 'dog', 'mouse']))
# []
print(all_construct2('', ['cat', 'dog', 'mouse']))
# [[]]
print(all_construct2('purple', ['purp', 'p', 'ur', 'le', 'purpl']))
# [['le', 'purp'], ['le', 'p', 'ur', 'p']]
print(all_construct2('abcdef', ['ab', 'abc', 'cd', 'def', 'abcd', 'ef', 'c']))
# [['ef', 'cd', 'ab'], ['def', 'c', 'ab'], ['def', 'abc'], ['ef', 'abcd']]