Skip to content

Instantly share code, notes, and snippets.

@KyleJamesWalker
Created February 7, 2015 01:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KyleJamesWalker/78ea6dc77e607250e15e to your computer and use it in GitHub Desktop.
Save KyleJamesWalker/78ea6dc77e607250e15e to your computer and use it in GitHub Desktop.
rangecounter.py
'''
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