-
Notifications
You must be signed in to change notification settings - Fork 0
/
perm_missing_elem.py
69 lines (50 loc) · 1.86 KB
/
perm_missing_elem.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# -*- coding: utf-8 -*-
"""
Author: Niall O'Connor
# https://app.codility.com/programmers/lessons/3-time_complexity/perm_missing_elem/
An array A consisting of N different integers is given. The array contains integers
in the range [1..(N + 1)], which means that exactly one element is missing.
Your goal is to find that missing element.
Write a function:
def solution(A)
that, given an array A, returns the value of the missing element.
For example, given array A such that:
A[0] = 2
A[1] = 3
A[2] = 1
A[3] = 5
the function should return 4, as it is the missing element.
Write an efficient algorithm for the following assumptions:
N is an integer within the range [0..100,000];
the elements of A are all distinct;
each element of array A is an integer within the range [1..(N + 1)].
# 100% solution https://app.codility.com/demo/results/trainingX8QHZM-3QN/
"""
import time
def solution(A):
"""
Euler states the sum of all integers is n(n+1)/2
Knowing what sum is expected verses given allows us to determining
the missing integer.
"""
given = sum(A)
n = len(A) + 1 # n is the length the array should be with no number missing!
expected = int((n*(n+1))//2)
return expected - given
if __name__ == '__main__':
tests = (
# Test cases are in pairs of (expected, (args,))
(5, ([1, 2, 3, 4, 6, 7, 8, 9, 10],)),
)
for expected, args in tests:
# record performance of solution
tic = time.perf_counter()
res = solution(*args)
toc = time.perf_counter()
print(f'ARGS produced {res} in {toc - tic:0.8f} seconds')
if args[0] is None:
continue # This is just a speed test
try:
assert(expected == res)
except AssertionError as e:
print(f'ERROR {args} produced {res} when {expected} was expected!')