-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
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))) | ||
""" | ||
for n in range(0,len(A)): | ||
if A[n] != B[n]: | ||
return False | ||
return True | ||
|
||
|
||
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])] | ||
for i in range(0, len(arr)-k): | ||
result.append(result[i] + arr[i+k] - arr[i]) | ||
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)) | ||
|