aboutsummaryrefslogtreecommitdiff
path: root/challenge-208/spadacciniweb/python/ch-1.py
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-208/spadacciniweb/python/ch-1.py')
-rw-r--r--challenge-208/spadacciniweb/python/ch-1.py56
1 files changed, 56 insertions, 0 deletions
diff --git a/challenge-208/spadacciniweb/python/ch-1.py b/challenge-208/spadacciniweb/python/ch-1.py
new file mode 100644
index 0000000000..2bdf122cbe
--- /dev/null
+++ b/challenge-208/spadacciniweb/python/ch-1.py
@@ -0,0 +1,56 @@
+# Task 1: Minimum Index Sum
+# Submitted by: Mohammad S Anwar
+#
+# You are given two arrays of strings.
+# Write a script to find out all common strings in the given two arrays with minimum index sum. If no common strings found returns an empty list.
+#
+# Example 1
+# Input: @list1 = ("Perl", "Raku", "Love")
+# @list2 = ("Raku", "Perl", "Hate")
+# Output: ("Perl", "Raku")
+#
+# There are two common strings "Perl" and "Raku".
+# Index sum of "Perl": 0 + 1 = 1
+# Index sum of "Raku": 1 + 0 = 1
+#
+# Example 2
+# Input: @list1 = ("A", "B", "C")
+# @list2 = ("D", "E", "F")
+# Output: ()
+#
+# No common string found, so no result.
+#
+# Example 3
+# Input: @list1 = ("A", "B", "C")
+# @list2 = ("C", "A", "B")
+# Output: ("A")
+#
+# There are three common strings "A", "B" and "C".
+# Index sum of "A": 0 + 1 = 1
+# Index sum of "B": 1 + 2 = 3
+# Index sum of "C": 2 + 0 = 2
+
+def get_minimum_index_sum(list1, list2):
+ words = set()
+ value = None
+ for i in range(0, len(list1)):
+ for j in range(0, len(list2)):
+ if value != None and value < i + j:
+ break
+ if list1[i] == list2[j]:
+ if value == None or value > i+j:
+ words = set()
+ value = i + j
+ words.add('"'+list1[i]+'"')
+ break
+ print("({:s})".format(', '.join(words)))
+
+if __name__ == "__main__":
+ get_minimum_index_sum(["Perl", "Raku", "Love"],
+ ["Raku", "Perl", "Hate"]);
+ get_minimum_index_sum(["A", "B", "C"],
+ ["D", "E", "F"]
+ );
+ get_minimum_index_sum(["A", "B", "C"],
+ ["C", "A", "B"]
+ );