-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoving-average.py
50 lines (35 loc) · 1.16 KB
/
moving-average.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
47
48
49
50
'''
Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window.
Example:
MovingAverage m = new MovingAverage(3);
m.next(1) = 1
m.next(10) = (1 + 10) / 2
m.next(3) = (1 + 10 + 3) / 3
m.next(5) = (10 + 3 + 5) / 3
If you can work your way through that problem, be prepared for follow up questions:
- If all integer numbers from the stream are between 0 and 100, how would you optimize it?
- If 99% of all integer numbers from the stream are between 0 and 100, how would you optimize it?
'''
class MovingAverage:
data = []
def __init__(self, windowSize):
self.windowSize = windowSize
def next(self, num):
self.data.append(num)
sum = 0
if len(self.data) < self.windowSize:
for i in range(0, len(self.data)):
sum += self.data[i]
return sum/len(self.data)
else:
start = len(self.data) - self.windowSize
end = start + self.windowSize
for i in range(start, end):
sum += self.data[i]
return sum/self.windowSize
movingAverage = MovingAverage(3)
#print(movingAverage.windowSize)
print(movingAverage.next(1))
print(movingAverage.next(10))
print(movingAverage.next(3))
print(movingAverage.next(5))