-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.py
25 lines (18 loc) · 971 Bytes
/
solution.py
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
# You're working in a number zoo, and it seems that one of the numbers has gone missing!
# Zoo workers have no idea what number is missing, and are too incompetent to figure it out,
# so they're hiring you to do it for them.
# In case the zoo loses another number, they want your program to work regardless of how many numbers there are in total.
# Task:
# Write a function that takes a shuffled list of unique numbers from 1 to n with one element missing
# (which can be any number including n).
# Return this missing number.
# Note: huge lists will be tested.
# Examples:
# [1, 3, 4] => 2
# [1, 2, 3] => 4
# [4, 2, 3] => 1
def find_missing_number(numbers):
# return list(set(range(1, len(numbers) + 2)) - set(numbers))[0] - about 10 seconds of all tests
# return list(set(range(1, len(numbers) + 2)) ^ set(numbers))[0] - about 10 seconds of all tests
n = len(numbers) + 1
return n * (n + 1) // 2 - sum(numbers) # about 6 seconds all tests