diff options
| author | Mohammad Sajid Anwar <mohammad.anwar@yahoo.com> | 2024-08-19 19:23:18 +0100 |
|---|---|---|
| committer | Mohammad Sajid Anwar <mohammad.anwar@yahoo.com> | 2024-08-19 19:23:18 +0100 |
| commit | 0d16a77607edcc8ca2e2c86efe6e63de7c96d148 (patch) | |
| tree | f032a0e3274edaff60c0a5556356687c7597f093 | |
| parent | 67d65b8afaaad7d236f74a639888ba1c09a5c79c (diff) | |
| parent | 871845f485e411d62425ae08916721137b9daeb2 (diff) | |
| download | perlweeklychallenge-club-0d16a77607edcc8ca2e2c86efe6e63de7c96d148.tar.gz perlweeklychallenge-club-0d16a77607edcc8ca2e2c86efe6e63de7c96d148.tar.bz2 perlweeklychallenge-club-0d16a77607edcc8ca2e2c86efe6e63de7c96d148.zip | |
Merge remote-tracking branch 'refs/remotes/origin/master'
| -rw-r--r-- | challenge-283/steven-wilson/python/ch-1.py | 34 | ||||
| -rw-r--r-- | challenge-283/steven-wilson/python/ch-2.py | 32 |
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) |
