aboutsummaryrefslogtreecommitdiff
path: root/challenge-283/packy-anderson/python/ch-1.py
blob: dde2091f5cd4e31d4f99eace411c3f2074404161 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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])