-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.py
25 lines (20 loc) · 1.1 KB
/
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
# An Arithmetic Progression is defined as one in which there is a constant difference between the consecutive terms of a
# given series of numbers.
# You are provided with consecutive elements of an Arithmetic Progression.
# There is however one hitch: exactly one term from the original series is missing
# from the set of numbers which have been given to you.
# The rest of the given series is the same as the original AP.
# Find the missing term.
# You have to write a function that receives a list, list size will always be at least 3 numbers.
# The missing term will never be the first or last one.
# Example
# find_missing([1, 3, 5, 9, 11]) == 7
# PS: This is a sample question of the facebook engineer challenge on interviewstreet.
# I found it quite fun to solve on paper using math, derive the algo that way. Have fun!
def find_missing(sequence):
# a, b = sequence[0], sequence[-1]
# step = sequence[-1] - sequence[-2]
# seq = [x for x in range(a, b + 1, step)]
#
# return list(set(seq) - set(sequence))[0]
return (sequence[0] + sequence[-1]) * (len(sequence) + 1) / 2 - sum(sequence)