-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmissing.py
More file actions
30 lines (22 loc) · 768 Bytes
/
missing.py
File metadata and controls
30 lines (22 loc) · 768 Bytes
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
"""Given a list of numbers 1...max_num, find which one is missing in a list."""
def missing_number(nums, max_num):
"""Given a list of numbers 1...max_num, find which one is missing.
*nums*: list of numbers 1..[max_num]; exactly one digit will be missing.
*max_num*: Largest potential number in list
>>> missing_number([7, 3, 2, 4, 5, 6, 1, 9, 10], 10)
8
"""
counter = 1
nums = sorted(nums)
for num in nums:
if num != counter:
return num - 1
counter += 1
if counter == max_num:
return max_num
else:
raise Exception("None are missing!")
if __name__ == '__main__':
import doctest
if doctest.testmod().failed == 0:
print "\n*** ALL TESTS PASS. NICELY DONE!\n"