From 196b6c270e7f0bbe188c40093451fca3dd52c57e Mon Sep 17 00:00:00 2001 From: Walt Mankowski Date: Wed, 26 Aug 2020 09:15:54 -0400 Subject: python solution for challenge 75 task 1 first draft --- challenge-075/walt-mankowski/python/ch-1.py | 36 +++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 challenge-075/walt-mankowski/python/ch-1.py (limited to 'challenge-075/walt-mankowski/python') diff --git a/challenge-075/walt-mankowski/python/ch-1.py b/challenge-075/walt-mankowski/python/ch-1.py new file mode 100644 index 0000000000..b070bcb102 --- /dev/null +++ b/challenge-075/walt-mankowski/python/ch-1.py @@ -0,0 +1,36 @@ +from sys import argv +from copy import deepcopy +import numpy as np + +s = int(argv[1]) +c = np.array([int(x) for x in argv[2:]]) +solutions = [] + +cnt = np.array([0 for _ in range(len(c))]) +while (True): + val = np.dot(c, cnt) + if val >= s: + if val == s: + solutions.append(deepcopy(cnt)) + + # rotate "odometer" + cnt[-1] = 0 + j = -2 + cnt[j] += 1 + while j >= -len(cnt) and np.dot(c, cnt) > s: + cnt[j] = 0 + j -= 1 + if j >= -len(cnt): + cnt[j] += 1 + + if j < -len(cnt): + break + else: + cnt[-1] += 1 + +print("There are", len(solutions), "ways to make sum", s) +for sol in solutions: + tmp = [] + for i in range(len(c)): + tmp += [c[i] for _ in range (sol[i])] + print(tmp) -- cgit