aboutsummaryrefslogtreecommitdiff
path: root/challenge-284/steven-wilson/python/ch-1.py
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-284/steven-wilson/python/ch-1.py')
-rw-r--r--challenge-284/steven-wilson/python/ch-1.py30
1 files changed, 30 insertions, 0 deletions
diff --git a/challenge-284/steven-wilson/python/ch-1.py b/challenge-284/steven-wilson/python/ch-1.py
new file mode 100644
index 0000000000..6e38592c6a
--- /dev/null
+++ b/challenge-284/steven-wilson/python/ch-1.py
@@ -0,0 +1,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)