aboutsummaryrefslogtreecommitdiff
path: root/challenge-212/sgreen/python/ch-2.py
diff options
context:
space:
mode:
authorSimon Green <mail@simon.green>2023-04-16 23:03:51 +1000
committerSimon Green <mail@simon.green>2023-04-16 23:03:51 +1000
commit2a42af32e400849b88d49d4851183ffdb503141e (patch)
treee04a19f6a4b9149c9ab24ea0efcf1ac7bee14549 /challenge-212/sgreen/python/ch-2.py
parent6d9f9d89ba086af0198b7d335c046e4096d38fde (diff)
downloadperlweeklychallenge-club-2a42af32e400849b88d49d4851183ffdb503141e.tar.gz
perlweeklychallenge-club-2a42af32e400849b88d49d4851183ffdb503141e.tar.bz2
perlweeklychallenge-club-2a42af32e400849b88d49d4851183ffdb503141e.zip
Simon's solution to challenge 212
Diffstat (limited to 'challenge-212/sgreen/python/ch-2.py')
-rwxr-xr-xchallenge-212/sgreen/python/ch-2.py41
1 files changed, 41 insertions, 0 deletions
diff --git a/challenge-212/sgreen/python/ch-2.py b/challenge-212/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..9aeb60e079
--- /dev/null
+++ b/challenge-212/sgreen/python/ch-2.py
@@ -0,0 +1,41 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def main(array):
+ # Take off the last value, and sort the remaining numbers
+ n = array.pop()
+ array = sorted(array)
+ solution = []
+
+ while array:
+ # Get the smallest number, and create a set with expected numbers
+ expected = range(array[0], array[0]+n)
+ not_matched = set(expected)
+
+ # Create an new array less the first instance of each number in the set
+ new_array = []
+ for num in array:
+ if num in not_matched:
+ not_matched.remove(num)
+ else:
+ new_array.append(num)
+
+ if not_matched:
+ # We don't have the expected numbers
+ print('-1')
+ return
+
+ # We have a solution
+ solution.append('(' + ','.join([str(x) for x in expected]) + ')')
+ array = new_array
+
+ # Print the solution
+ print(', '.join(solution))
+
+
+if __name__ == '__main__':
+ # Turn the strings into integers
+ n = [int(i) for i in sys.argv[1:]]
+ main(n)