From 2a42af32e400849b88d49d4851183ffdb503141e Mon Sep 17 00:00:00 2001 From: Simon Green Date: Sun, 16 Apr 2023 23:03:51 +1000 Subject: Simon's solution to challenge 212 --- challenge-212/sgreen/python/ch-2.py | 41 +++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100755 challenge-212/sgreen/python/ch-2.py (limited to 'challenge-212/sgreen/python') 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) -- cgit