diff options
| author | Packy Anderson <packy@cpan.org> | 2024-08-20 02:12:17 -0400 |
|---|---|---|
| committer | Packy Anderson <packy@cpan.org> | 2024-08-20 02:12:17 -0400 |
| commit | 5b21193086ff20b64b19f33112cd57a2a3a0077b (patch) | |
| tree | 820d02d644e4b7384777e17e11cf17803acb05f3 /challenge-283/packy-anderson/python/ch-1.py | |
| parent | 094015310b8f295a6d758b8f39788958f9687730 (diff) | |
| download | perlweeklychallenge-club-5b21193086ff20b64b19f33112cd57a2a3a0077b.tar.gz perlweeklychallenge-club-5b21193086ff20b64b19f33112cd57a2a3a0077b.tar.bz2 perlweeklychallenge-club-5b21193086ff20b64b19f33112cd57a2a3a0077b.zip | |
Challenge 283 solutions by Packy Anderson
* Raku that maybe looks like Raku, but mostly like Perl
* Perl
* Python that definitely looks like Perl
* Elixir
1 Blog post
Diffstat (limited to 'challenge-283/packy-anderson/python/ch-1.py')
| -rwxr-xr-x | challenge-283/packy-anderson/python/ch-1.py | 45 |
1 files changed, 45 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]) |
