aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-08-19 19:22:39 +0100
committerGitHub <noreply@github.com>2024-08-19 19:22:39 +0100
commit871845f485e411d62425ae08916721137b9daeb2 (patch)
tree9f129f2d6f3c373b8129376cd4d68448f1ef99cf
parent822cc9f0892e4bbfe82be0fb11a438e84e57ccdc (diff)
parent4b25c965957cff92bc4149ec735926e02daacaeb (diff)
downloadperlweeklychallenge-club-871845f485e411d62425ae08916721137b9daeb2.tar.gz
perlweeklychallenge-club-871845f485e411d62425ae08916721137b9daeb2.tar.bz2
perlweeklychallenge-club-871845f485e411d62425ae08916721137b9daeb2.zip
Merge pull request #10662 from oWnOIzRi/week283
add solutions week 283 in python
-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)