aboutsummaryrefslogtreecommitdiff
path: root/challenge-283/packy-anderson/python/ch-2.py
blob: e8ccd8b72cbee223850c45eee73e8d03fb297bae (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
46
47
48
49
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])