diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-08-20 08:12:02 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-20 08:12:02 +0100 |
| commit | c9e2021e40366f295331ec7c4b2be2be876e6eaa (patch) | |
| tree | 4514553f20d642976fa59ef97d483fe9091a75a9 /challenge-283/packy-anderson/python | |
| parent | 70acc6b56322e68ed9b4550fd6e66f6c5c9353e9 (diff) | |
| parent | 5b21193086ff20b64b19f33112cd57a2a3a0077b (diff) | |
| download | perlweeklychallenge-club-c9e2021e40366f295331ec7c4b2be2be876e6eaa.tar.gz perlweeklychallenge-club-c9e2021e40366f295331ec7c4b2be2be876e6eaa.tar.bz2 perlweeklychallenge-club-c9e2021e40366f295331ec7c4b2be2be876e6eaa.zip | |
Merge pull request #10669 from packy/master
Challenge 283 solutions by Packy Anderson
Diffstat (limited to 'challenge-283/packy-anderson/python')
| -rwxr-xr-x | challenge-283/packy-anderson/python/ch-1.py | 45 | ||||
| -rwxr-xr-x | challenge-283/packy-anderson/python/ch-2.py | 50 |
2 files changed, 95 insertions, 0 deletions
diff --git a/challenge-283/packy-anderson/python/ch-1.py b/challenge-283/packy-anderson/python/ch-1.py new file mode 100755 index 0000000000..dde2091f5c --- /dev/null +++ b/challenge-283/packy-anderson/python/ch-1.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python + +from collections import Counter + +def uniqueNumber(ints): + bag = Counter(ints) # count occurrences + keys = [ + k # return just the keys + for k,v in bag.items() # get key/value pairs + if v == 1 # keys that occur once + ] + + # invalid input + if len(keys) == 0: + return 'no element appears only once' + if len(keys) > 1: + return 'multiple elements appear only once' + + # return the one value + return keys.pop(0) + +def comma_join(arr): + return ', '.join(map(lambda i: str(i), arr)) + +def solution(ints): + print(f'Input: @ints = ({comma_join(ints)})') + print(f'Output: {uniqueNumber(ints)}') + +print('Example 1:') +solution([3, 3, 1]) + +print('\nExample 2:') +solution([3, 2, 4, 2, 4]) + +print('\nExample 3:') +solution([1]) + +print('\nExample 4:') +solution([4, 3, 1, 1, 1, 4]) + +print('\nInvalid Input 1 (no element appears only once):') +solution([4, 1, 1, 1, 4]) + +print('\nInvalid Input 2 (multiple elements appear only once):') +solution([1, 2, 3, 4]) diff --git a/challenge-283/packy-anderson/python/ch-2.py b/challenge-283/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..e8ccd8b72c --- /dev/null +++ b/challenge-283/packy-anderson/python/ch-2.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python + +from collections import Counter + +def digitCountValue(ints): + bag = Counter(ints) # count occurrences + explain = [] + passes = True + for digit in range(len(ints)): + times = ints[digit] + occurs = bag[digit] + otimes = ( + "0 times" if occurs == 0 else + f"{occurs} times" if occurs >= 2 else + "1 time" + ) + if times == occurs: + explain.append( + f"$ints[{digit}] = {times}, the digit {digit} " + + f"occurs {otimes}" + ) + else: + passes = False + ttimes = ( + "0 times" if times == 0 else + f"{times} times" if times >= 2 else + "1 time" + ) + explain.append( + f"$ints[{digit}] = {times}, the digit {digit} " + + f"occurs {otimes} rather than {ttimes}" + ) + return passes, "\n".join(explain) + +def comma_join(arr): + return ', '.join(map(lambda i: str(i), arr)) + +def solution(ints): + print(f'Input: @ints = ({comma_join(ints)})') + passes, explain = digitCountValue(ints) + print(f'Output: {passes}\n\n{explain}') + +print('Example 1:') +solution([1, 2, 1, 0]) + +print('\nExample 2:') +solution([0, 3, 0]) + +print('\nExample 3:') +solution([0, 1, 2, 2]) |
