aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-283/steven-wilson/python/ch-1.py34
-rw-r--r--challenge-283/steven-wilson/python/ch-2.py32
2 files changed, 66 insertions, 0 deletions
diff --git a/challenge-283/steven-wilson/python/ch-1.py b/challenge-283/steven-wilson/python/ch-1.py
new file mode 100644
index 0000000000..d95b9b5534
--- /dev/null
+++ b/challenge-283/steven-wilson/python/ch-1.py
@@ -0,0 +1,34 @@
+#!/usr/bin/env python3
+
+from collections import Counter
+
+
+def unique_number(integers):
+ """ Given an array of integers, where every element appears more than once
+ except one element, return the one element that appears exactly one time.
+
+ >>> unique_number([3, 3, 1])
+ 1
+ >>> unique_number([3, 2, 4, 2, 4])
+ 3
+ >>> unique_number([1])
+ 1
+ >>> unique_number([4, 3, 1, 1, 1, 4])
+ 3
+ """
+ if not all(isinstance(i, int) for i in integers):
+ raise ValueError('The array should only contain integers')
+
+ counter = Counter(integers)
+ unique = [k for k, v in counter.items() if v == 1]
+
+ if len(unique) != 1:
+ raise ValueError('There should be one unique element in the array')
+
+ return unique[0]
+
+
+if __name__ == "__main__":
+ import doctest
+
+ doctest.testmod(verbose=True)
diff --git a/challenge-283/steven-wilson/python/ch-2.py b/challenge-283/steven-wilson/python/ch-2.py
new file mode 100644
index 0000000000..7092799907
--- /dev/null
+++ b/challenge-283/steven-wilson/python/ch-2.py
@@ -0,0 +1,32 @@
+#!/usr/bin/env python3
+
+from collections import Counter
+
+
+def digit_count_value(integers):
+ """ Given an array of positive integers, return true if for every index i
+ in the range 0 <= i < size of array, the digit i occurs exactly the ints[i]
+ times in the given array otherwise return false.
+
+ >>> digit_count_value([1, 2, 1, 0])
+ True
+ >>> digit_count_value([0, 3, 0])
+ False
+ """
+ if not all(isinstance(i, int) for i in integers):
+ raise ValueError("Array values should be integers")
+
+ if not all(i >= 0 for i in integers):
+ raise ValueError("Array values should be positive")
+
+ counter = Counter(integers)
+ for n, i in enumerate(integers):
+ if i != counter[n]:
+ return False
+ return True
+
+
+if __name__ == "__main__":
+ import doctest
+
+ doctest.testmod(verbose=True)