-
Notifications
You must be signed in to change notification settings - Fork 11
/
array.py
47 lines (39 loc) · 1.12 KB
/
array.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
list_compare0 = lambda A,B: all(A[n] == B[n] for n in range(0, len(A)))
def list_compare1(A,B):
"""
Compare elementwise two lists.
Args:
Lists: A,B.
Returns:
Boolean
Comment:
Still faster than: return all(A[n] == B[n] for n in range(0, len(A)))
"""
return all(A[n] == B[n] for n in range(0, len(A)))
def list_compare2(A,B):
tmp = 0
n = 0
lA=len(A)
while tmp == 0 and n < lA:
tmp += abs(A[n]-B[n])
n += 1
return n == lA
def fixed_sliding_window(arr, k):
result = [sum(arr[:k])]
result.extend(result[i] + arr[i+k] - arr[i] for i in range(0, len(arr)-k))
return result
print(fixed_sliding_window(list(range(1,7)),3))
def dynamic_sliding_window(arr, x):
min_length = len(arr)
start = 0
end = 0
current_sum = 0
while end < min_length:
current_sum += arr[end]
end += 1
while start < end and current_sum >= x:
current_sum -= arr[start]
start += 1
min_length = min(min_length, end-start+1)
return min_length
print(dynamic_sliding_window(list(range(1,7)), 7))