aboutsummaryrefslogtreecommitdiff
path: root/challenge-191/sgreen/python/ch-2.py
diff options
context:
space:
mode:
authorSimon Green <mail@simon.green>2022-11-17 22:50:07 +1100
committerSimon Green <mail@simon.green>2022-11-17 22:50:07 +1100
commit8203bca3a019823f328b70720bf4168b9eefc5d3 (patch)
tree3f70ab43da46ad784b72204f7ce2d1ec07ed19fd /challenge-191/sgreen/python/ch-2.py
parent0df63088f99a782b77e5661449ec508ab5940035 (diff)
downloadperlweeklychallenge-club-8203bca3a019823f328b70720bf4168b9eefc5d3.tar.gz
perlweeklychallenge-club-8203bca3a019823f328b70720bf4168b9eefc5d3.tar.bz2
perlweeklychallenge-club-8203bca3a019823f328b70720bf4168b9eefc5d3.zip
Simon's solution to challenge 191
Diffstat (limited to 'challenge-191/sgreen/python/ch-2.py')
-rwxr-xr-xchallenge-191/sgreen/python/ch-2.py32
1 files changed, 32 insertions, 0 deletions
diff --git a/challenge-191/sgreen/python/ch-2.py b/challenge-191/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..6c610e6d88
--- /dev/null
+++ b/challenge-191/sgreen/python/ch-2.py
@@ -0,0 +1,32 @@
+#!/usr/bin/env python3
+
+import sys
+
+def find_solutions(front, back):
+ '''Recursive function to find possible solutions. Front is the numbers we
+ have processed, back is what remains'''
+ if len(back) == 0:
+ return 1
+
+ # Iterate over the next value
+ count = 0
+ for i in back[0]:
+ # Call the recursive function if we haven't used this number already
+ if i not in front:
+ count += find_solutions([*front, i], back[1:])
+
+ # Return the number of matches up the recursive function
+ return count
+
+def main(n):
+ '''Main function'''
+ # Generate a list of list with a possible value for each number
+ possible = []
+ for i in range(1, n+1):
+ possible.append([j for j in range(1, n+1) if j % i == 0 or i % j == 0])
+
+ print(find_solutions([], possible))
+
+
+if __name__ == '__main__':
+ main(int(sys.argv[1]))