aboutsummaryrefslogtreecommitdiff
path: root/challenge-109/stuart-little/python/ch-2.py
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-04-19 23:28:30 +0100
committerGitHub <noreply@github.com>2021-04-19 23:28:30 +0100
commit78b641b69a5133a9c8f79462f9ccda0f2f5b8595 (patch)
treee2533fcc3492d70170c655b1768d35bed17bb6c8 /challenge-109/stuart-little/python/ch-2.py
parentbb2f1a7bea02a486b41e446d7496630bf10341c2 (diff)
parent2abf6310e945c12e3138493ac44004dc7e078196 (diff)
downloadperlweeklychallenge-club-78b641b69a5133a9c8f79462f9ccda0f2f5b8595.tar.gz
perlweeklychallenge-club-78b641b69a5133a9c8f79462f9ccda0f2f5b8595.tar.bz2
perlweeklychallenge-club-78b641b69a5133a9c8f79462f9ccda0f2f5b8595.zip
Merge pull request #3930 from stuart-little/stuart-little_109_python
1st commit on 109_python
Diffstat (limited to 'challenge-109/stuart-little/python/ch-2.py')
-rwxr-xr-xchallenge-109/stuart-little/python/ch-2.py21
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)))