diff options
Diffstat (limited to 'challenge-109/stuart-little/python/ch-2.py')
| -rwxr-xr-x | challenge-109/stuart-little/python/ch-2.py | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/challenge-109/stuart-little/python/ch-2.py b/challenge-109/stuart-little/python/ch-2.py new file mode 100755 index 0000000000..c0dfbf495d --- /dev/null +++ b/challenge-109/stuart-little/python/ch-2.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python + +# run <script> <space-separated numbers> + +from itertools import permutations +import string +import sys + +def sumsEqNr(nr,ar,bl): + if (len(ar) <= 3): + return bl and (nr == sum(ar)) + return sumsEqNr(nr,ar[2:],(bl and (nr == sum(ar[:3])))) + +def allSumsEq(ar): + return sumsEqNr(sum(ar[:2]),ar[1:],True) + +def pprnt(ar): + return "\nSolution: " + ", ".join([f"{c} = {v}" for (c,v) in list(zip(string.ascii_lowercase[:7],ar))]) + f"\nSum: {sum(ar[:2])}" + +sols = list(filter(allSumsEq,list(permutations(map(int, sys.argv[1:]))))) +print("No solution." if (not sols) else "\n".join(map(pprnt,sols))) |
