Skip to content

Instantly share code, notes, and snippets.

@timotheus
Created January 9, 2014 22:48
Show Gist options
  • Save timotheus/8343564 to your computer and use it in GitHub Desktop.
Save timotheus/8343564 to your computer and use it in GitHub Desktop.
import sys
def generate_results(result_set, columns=4):
total = sum([cnt+1 for ltr, cnt in result_set])
column_max = total / columns
column_count = 0
results = {}
bucket = 0
results[bucket] = []
while result_set:
t1, t2 = result_set.pop(0)
column_count += t2
if column_count > column_max:
bucket += 1
column_count = 0
results[bucket] = []
results[bucket].append((t1, t2))
else:
results[bucket].append((t1, t2))
return results
ca = [ ('a', 9), ('b', 12), ('c', 12), ('e', 6), ('d', 21), ('f', 12), ('g', 12), ('h', 6), ('i', 6), ('j', 10), ('k', 10), ('l', 20), ('m', 4)]
results = generate_results(ca, 4)
for i in range(4):
sys.stdout.write("bucket(%s): " % i)
for row in results[i]:
sys.stdout.write("(%s)" % row[0])
sys.stdout.write("+" * row[1])
sys.stdout.write("\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment