-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathr-5.7.py
37 lines (29 loc) · 779 Bytes
/
r-5.7.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
26
27
28
29
30
31
32
33
34
35
36
37
"""
R-5.7 Let A be an array of size n ≥ 2 containing integers from 1 to n−1, inclusive,
with exactly one repeated. Describe a fast algorithm for finding the integer in A that is repeated."""
import random
def sample(n=2):
assert n >= 2
a = [i for i in range(1, n)]
a.append(random.choice(a))
random.shuffle(a)
return a
def find1(l):
l.sort() # O(nlogn)
for i in range(1, len(l)):
if l[i] == l[i-1]:
return l[i]
return None
def find2(l):
"""
len(l) = 10
timeit 100000 loops, best of 3: 12.9 us per loop
len(l) = 1000
100000 loops, best of 3: 10.4 us per loop
"""
n = len(l) # O(1)
return sum(l) - (n*n - n) / 2 # O(n) for sum()
s = sample(10)
print(s)
print(find1(s))
print(find2(s))