blob: 6e38592c6a7d0c00376928265c59902ef531acfc (
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
|
#!/usr/bin/env python3
from collections import Counter
def lucky_integer(*integers):
""" Given an array of integers, return the lucky integer if found otherwise
return -1. If there are more than one then return the largest.
A lucky integer is an integer that has a frequency in the array equal
to its value.
>>> lucky_integer(2, 2, 3, 4)
2
>>> lucky_integer(1, 2, 2, 3, 3, 3)
3
>>> lucky_integer(1, 1, 1, 3)
-1
"""
counter = Counter(integers)
try:
return max(i for i in set(integers) if i == counter[i])
except ValueError:
return -1
if __name__ == "__main__":
import doctest
doctest.testmod(verbose=True)
|