aboutsummaryrefslogtreecommitdiff
path: root/challenge-212/sgreen/python
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-212/sgreen/python')
-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)