forked from jamil-said/code-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeviation.py
More file actions
executable file
·100 lines (84 loc) · 2.84 KB
/
deviation.py
File metadata and controls
executable file
·100 lines (84 loc) · 2.84 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
""" deviation
Given an array of integer elements and an integer d please consider all
the sequences of d consecutive elements in the array. For each sequence
we compute the difference between the maximum and the minimum value of
the elements in that sequence and name it the deviation.
Your task is to
write a function that computes the maximum value among the deviations of
all the sequences considered above
print the value the standard output (stdout)
Note that your function will receive the following arguments:
v
which is the array of integers
d
which is an integer value giving the length of the sequences
Data constraints
the array will contain up to 100,000 elements
all the elements in the array are integer numbers in the following range:
[1, 2^31 -1]
the value of d will not exceed the length of the given array
Efficiency constraints
your function is expected to print the result in less than 2 seconds
Example Input Output
v: 6, 9, 4, 7, 4, 1 6
d: 3
"""
def deviation(v, d):
drop, maxD, tMax, tMin = 0, float('-inf'), float('-inf'), float('inf')
for i in range(len(v)-d+1):
if tMin < drop < tMax and tMin <= v[i+d-1] <= tMax: continue
tMax, tMin, drop = max(v[i:i+d]), min(v[i:i+d]), v[i]
maxD = max(maxD, (tMax-tMin))
print(maxD)
""" brute force, slower
def deviation(v, d):
maxD = float('-inf')
for i in range(len(v)-d+1):
maxD = max(maxD, (max(v[i:i+d])-min(v[i:i+d])))
print(maxD)
"""
""" alternative, longer, slower
def deviation(v, d):
minSli, maxSli = [], []
for n in v[:d]:
while len(minSli) != 0 and minSli[-1] > n:
minSli.pop()
minSli.append(n)
while len(maxSli) != 0 and maxSli[-1] < n:
maxSli.pop()
maxSli.append(n)
maxD = maxSli[0] - minSli[0]
for index in range(d, len(v)):
tmpN, drop = v[index], v[index-d]
if maxSli[0] == drop: maxSli.remove(drop)
if minSli[0] == drop: minSli.remove(drop)
while len(minSli) != 0 and minSli[-1] > tmpN:
minSli.pop()
minSli.append(tmpN)
while len(maxSli) != 0 and maxSli[-1] < tmpN:
maxSli.pop()
maxSli.append(tmpN)
maxD = max(maxD, (maxSli[0]-minSli[0]))
print(maxD)
"""
deviation([6, 9, 4, 7, 4, 1], 3) #6
deviation([6, 9, 4, 7, 4, 1], 6) #8
deviation([6, 9, 4, 7, 4, 1, 4, 5, 1, 2], 3) #6
deviation([1, 2, 3, 4, 5, 6], 2) #1
deviation([6, 9, 4, 9, 4, 7, 7, 4, 1], 3) #6
deviation([1], 1) #0
""" test module (big numbers)
import time
import random
def testF():
count = 1
random.seed()
for i in range(count):
test = random.sample(range(10000000), 1000000)
testlen = random.randint(2,50)
start = time.time()
deviation(test, testlen)
end = time.time()
print('last result time in seconds:', end - start)
testF()
"""