aboutsummaryrefslogtreecommitdiff
path: root/challenge-208/sgreen/python/ch-1.py
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-208/sgreen/python/ch-1.py')
-rwxr-xr-xchallenge-208/sgreen/python/ch-1.py49
1 files changed, 49 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])