Skip to content

Commit 7248a72

Browse files
authored
Create Moving Window.py
1 parent 3d180cc commit 7248a72

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

interview_query/Moving Window.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Given a list of numbers nums and an integer window_size, write a function moving_window to find the moving window average.
2+
3+
4+
5+
def moving_window(input_list,window_size):
6+
7+
window_start = 0
8+
window_end = 0
9+
10+
res = 0
11+
12+
out = []
13+
14+
while window_end< len(input_list):
15+
res += input_list[window_end]
16+
out.append(res/(window_end-window_start+1))
17+
18+
window_end+=1
19+
20+
if window_end-window_start>=window_size:
21+
res -= input_list[window_start]
22+
window_start+=1
23+
24+
return out

0 commit comments

Comments
 (0)