aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-268/steven-wilson/python/ch-1.py30
-rw-r--r--challenge-268/steven-wilson/python/ch-2.py32
2 files changed, 62 insertions, 0 deletions
diff --git a/challenge-268/steven-wilson/python/ch-1.py b/challenge-268/steven-wilson/python/ch-1.py
new file mode 100644
index 0000000000..e314318011
--- /dev/null
+++ b/challenge-268/steven-wilson/python/ch-1.py
@@ -0,0 +1,30 @@
+#!/usr/bin/env python3
+
+
+def magic_number(x, y):
+ ''' Given two arrays of integers of same size, find the magic number that
+ when added to each elements of one of the array gives the second array.
+ Elements order is not important.
+ >>> magic_number([3, 7, 5], [9, 5, 7])
+ 2
+ >>> magic_number([1, 2, 1], [5, 4, 4])
+ 3
+ >>> magic_number([2], [5])
+ 3
+ '''
+ if not isinstance(x, list) or not isinstance(y, list):
+ raise ValueError("Inputs 'x' and 'y' must be lists.")
+
+ if len(x) != len(y):
+ raise ValueError("Inputs 'x' and 'y' must have the same length.")
+
+ if not all(isinstance(num, int) for num in x) or not all(isinstance(num, int) for num in y):
+ raise ValueError("All elements in 'x' and 'y' must be integers.")
+
+ return abs(max(x) - max(y))
+
+
+if __name__ == "__main__":
+ import doctest
+
+ doctest.testmod(verbose=True)
diff --git a/challenge-268/steven-wilson/python/ch-2.py b/challenge-268/steven-wilson/python/ch-2.py
new file mode 100644
index 0000000000..8f24234274
--- /dev/null
+++ b/challenge-268/steven-wilson/python/ch-2.py
@@ -0,0 +1,32 @@
+#!/usr/bin/env python3
+
+
+def number_game(elements):
+ ''' Given an array of integers, with even number of elements, create a new
+ array made up of elements of the given array. Pick the two smallest
+ integers and add it to new array in decreasing order i.e. high to low.
+ Keep doing until the given array is empty.
+ >>> number_game([2, 5, 3, 4])
+ [3, 2, 5, 4]
+ >>> number_game([9, 4, 1, 3, 6, 4, 6, 1])
+ [1, 1, 4, 3, 6, 4, 9, 6]
+ >>> number_game([1, 2, 2, 3])
+ [2, 1, 3, 2]
+ '''
+ if not isinstance(elements, list):
+ raise ValueError("Input 'elements' must be a list.")
+
+ if len(elements) % 2 != 0:
+ raise ValueError("Input 'elements' must have an even number of elements.")
+
+ if not all(isinstance(x, int) for x in elements):
+ raise ValueError("All elements in the input list must be integers.")
+
+ elem_iter = iter(sorted(elements))
+ return [item for pair in zip(elem_iter, elem_iter) for item in reversed(pair)]
+
+
+if __name__ == "__main__":
+ import doctest
+
+ doctest.testmod(verbose=True)