diff options
Diffstat (limited to 'challenge-208/sgreen/python')
| -rwxr-xr-x | challenge-208/sgreen/python/ch-1.py | 49 | ||||
| -rwxr-xr-x | challenge-208/sgreen/python/ch-2.py | 27 |
2 files changed, 76 insertions, 0 deletions
diff --git a/challenge-208/sgreen/python/ch-1.py b/challenge-208/sgreen/python/ch-1.py new file mode 100755 index 0000000000..9ab9f318d4 --- /dev/null +++ b/challenge-208/sgreen/python/ch-1.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python3 + +import re +import sys + + +def find_index_in_list(array, word): + '''Find the index of a word in a list''' + for i, w in enumerate(array): + if w == word: + return i + + # This word does not appear in the list + return None + + +def main(first_list, second_list): + # Turn the words into a list + first_list = re.findall(r'\w+', first_list) + second_list = re.findall(r'\w+', second_list) + + # We set the index_sum to one more than the greatest possible solution + index_sum = len(first_list) + len(second_list) + 1 + solution = [] + + # Loop through the first_list + for i1, w in enumerate(first_list): + # See if it appears in the second list + i2 = find_index_in_list(second_list, w) + if i2 is not None: + # The the index sum of this word + i = i1 + i2 + if i < index_sum: + # It is better than the previous solution + solution = [w] + index_sum = i + elif i == index_sum: + # It is the same as the previous solution + solution.append(w) + + # Print the results + if solution: + print('("' + '", "'.join(solution) + '")') + else: + print('()') + + +if __name__ == '__main__': + main(sys.argv[1], sys.argv[2]) diff --git a/challenge-208/sgreen/python/ch-2.py b/challenge-208/sgreen/python/ch-2.py new file mode 100755 index 0000000000..84952f60f1 --- /dev/null +++ b/challenge-208/sgreen/python/ch-2.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 + +import sys + + +def main(array): + missing = None + duplicate = None + + for i in range(1, len(array)+1): + # Find out how many times this number appears in the list + count = sum(1 for n in array if n == i) + if count == 0: + missing = i + elif count > 1: + duplicate = i + + if missing is None: + print(-1) + else: + print(f'({duplicate or ""}, {missing})') + + +if __name__ == '__main__': + # Turn the strings into integers + n = [int(i) for i in sys.argv[1:]] + main(n) |
