Created
February 7, 2015 01:07
-
-
Save KyleJamesWalker/78ea6dc77e607250e15e to your computer and use it in GitHub Desktop.
rangecounter.py
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
''' | |
Simple class for counting number of occurances in each of the given ranges. | |
''' | |
class RangeCounter(object): | |
def __init__(self, range_list): | |
# Verify range is sorted | |
self.range_list = sorted(range_list) | |
self.counter = [0] * (len(range_list) + 1) | |
def add(self, view_count): | |
for i, val in enumerate(self.range_list): | |
if view_count <= val: | |
self.counter[i] += 1 | |
return | |
self.counter[-1] += 1 | |
matching_videos = RangeCounter([100, 1000, 5000, | |
50000, 1000000, 10000000]) | |
# This should make 7 counters | |
# 0 - 100 | |
# 101 - 1000 | |
# 1001 - 5000 | |
# 5001 - 50000 | |
# 50001 - 1000000 | |
# 1000001 - 10000000 | |
# 10000001+ | |
rows = [ | |
100, | |
1000, | |
5000, | |
50000, | |
1000000, | |
10000000, | |
100000000, | |
] | |
for the_number in rows: | |
matching_videos.add(the_number) | |
print matching_videos.counter |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment