From a23487532a52f58b7aa606fcda4a7429474524ad Mon Sep 17 00:00:00 2001 From: dclavijo Date: Wed, 3 Jan 2024 16:40:18 -0300 Subject: [PATCH] update array.py --- array.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 array.py diff --git a/array.py b/array.py new file mode 100644 index 0000000..46575fb --- /dev/null +++ b/array.py @@ -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)) +