Skip to content

Commit

Permalink
update array.py
Browse files Browse the repository at this point in the history
  • Loading branch information
daedalus committed Jan 3, 2024
1 parent c197483 commit a234875
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions array.py
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))

0 comments on commit a234875

Please sign in to comment.