Last active
December 18, 2015 08:39
-
-
Save dougwt/5755489 to your computer and use it in GitHub Desktop.
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
import collections | |
import random | |
class RollingAverage: | |
"""Generates a function that tracks a rolling average of a given size.""" | |
def __init__(self, size): | |
self.size = size | |
self.deque = collections.deque(list(), self.size) | |
self.count = 0 | |
def __call__(self, value): | |
"""Returns the average of the previous self.size values.""" | |
self.deque.append(value) | |
self.count += 1 | |
if self.count < self.size: | |
return None | |
return sum(self.deque) / float(self.size) | |
def main(): | |
"""Passes 50 random integers to a RollingAverage obj tracking 5 values.""" | |
five = RollingAverage(5) | |
for i in xrange(50): | |
value = random.randrange(0, 100) | |
print 'five(%s) = %s %s' % (str(value).rjust(2), five(value), five.deque) | |
if __name__ == '__main__': | |
main() |
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
five(14) = None deque([14], maxlen=5) | |
five(93) = None deque([14, 93], maxlen=5) | |
five(69) = None deque([14, 93, 69], maxlen=5) | |
five(64) = None deque([14, 93, 69, 64], maxlen=5) | |
five(50) = 58.0 deque([14, 93, 69, 64, 50], maxlen=5) | |
five(99) = 75.0 deque([93, 69, 64, 50, 99], maxlen=5) | |
five(78) = 72.0 deque([69, 64, 50, 99, 78], maxlen=5) | |
five(89) = 76.0 deque([64, 50, 99, 78, 89], maxlen=5) | |
five( 2) = 63.6 deque([50, 99, 78, 89, 2], maxlen=5) | |
five(30) = 59.6 deque([99, 78, 89, 2, 30], maxlen=5) | |
five(44) = 48.6 deque([78, 89, 2, 30, 44], maxlen=5) | |
five(54) = 43.8 deque([89, 2, 30, 44, 54], maxlen=5) | |
five(12) = 28.4 deque([2, 30, 44, 54, 12], maxlen=5) | |
five(39) = 35.8 deque([30, 44, 54, 12, 39], maxlen=5) | |
five(61) = 42.0 deque([44, 54, 12, 39, 61], maxlen=5) | |
five(44) = 42.0 deque([54, 12, 39, 61, 44], maxlen=5) | |
five(31) = 37.4 deque([12, 39, 61, 44, 31], maxlen=5) | |
five(41) = 43.2 deque([39, 61, 44, 31, 41], maxlen=5) | |
five(88) = 53.0 deque([61, 44, 31, 41, 88], maxlen=5) | |
five(49) = 50.6 deque([44, 31, 41, 88, 49], maxlen=5) | |
five(73) = 56.4 deque([31, 41, 88, 49, 73], maxlen=5) | |
five(51) = 60.4 deque([41, 88, 49, 73, 51], maxlen=5) | |
five(57) = 63.6 deque([88, 49, 73, 51, 57], maxlen=5) | |
five(24) = 50.8 deque([49, 73, 51, 57, 24], maxlen=5) | |
five(10) = 43.0 deque([73, 51, 57, 24, 10], maxlen=5) | |
five(70) = 42.4 deque([51, 57, 24, 10, 70], maxlen=5) | |
five( 7) = 33.6 deque([57, 24, 10, 70, 7], maxlen=5) | |
five(91) = 40.4 deque([24, 10, 70, 7, 91], maxlen=5) | |
five(59) = 47.4 deque([10, 70, 7, 91, 59], maxlen=5) | |
five(17) = 48.8 deque([70, 7, 91, 59, 17], maxlen=5) | |
five(53) = 45.4 deque([7, 91, 59, 17, 53], maxlen=5) | |
five(12) = 46.4 deque([91, 59, 17, 53, 12], maxlen=5) | |
five(84) = 45.0 deque([59, 17, 53, 12, 84], maxlen=5) | |
five( 7) = 34.6 deque([17, 53, 12, 84, 7], maxlen=5) | |
five(79) = 47.0 deque([53, 12, 84, 7, 79], maxlen=5) | |
five(92) = 54.8 deque([12, 84, 7, 79, 92], maxlen=5) | |
five(27) = 57.8 deque([84, 7, 79, 92, 27], maxlen=5) | |
five(34) = 47.8 deque([7, 79, 92, 27, 34], maxlen=5) | |
five(63) = 59.0 deque([79, 92, 27, 34, 63], maxlen=5) | |
five(91) = 61.4 deque([92, 27, 34, 63, 91], maxlen=5) | |
five(47) = 52.4 deque([27, 34, 63, 91, 47], maxlen=5) | |
five(98) = 66.6 deque([34, 63, 91, 47, 98], maxlen=5) | |
five(70) = 73.8 deque([63, 91, 47, 98, 70], maxlen=5) | |
five(95) = 80.2 deque([91, 47, 98, 70, 95], maxlen=5) | |
five(92) = 80.4 deque([47, 98, 70, 95, 92], maxlen=5) | |
five(98) = 90.6 deque([98, 70, 95, 92, 98], maxlen=5) | |
five(50) = 81.0 deque([70, 95, 92, 98, 50], maxlen=5) | |
five(93) = 85.6 deque([95, 92, 98, 50, 93], maxlen=5) | |
five(34) = 73.4 deque([92, 98, 50, 93, 34], maxlen=5) | |
five(95) = 74.0 deque([98, 50, 93, 34, 95], maxlen=5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment