-
Notifications
You must be signed in to change notification settings - Fork 0
/
3-can_sum.py
55 lines (41 loc) · 1.36 KB
/
3-can_sum.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import functools
import numbers
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 can_sum(target_sum, numbers = None):
if (target_sum == 0): return True
if (target_sum < 0): return False
for num in numbers:
remainder = target_sum - num
if (can_sum(remainder, numbers)):
return True
return False
print(can_sum(7, [2, 3])) # True
print(can_sum(7, [5, 3, 4, 7])) # True
print(can_sum(7, [2, 4])) # False
print(can_sum(8, [2, 3, 5])) # True
print(can_sum(1000, [7, 14])) # False
print("******* Tabulation *******")
def can_sum2(target_sum, numbers):
table = []
[table.append(False) for i in range(target_sum+1)]
table[0] = True
for i in range (len(table)):
if table[i]:
for num in numbers:
if (i+num) <= target_sum:
table[i+num] = True
return table[target_sum]
print(can_sum(7, [2, 3])) # True
print(can_sum2(7, [5, 3, 4, 7])) # True
print(can_sum2(7, [2, 4])) # False
print(can_sum2(8, [2, 3, 5])) # True
print(can_sum2(1000, [7, 14])) # False