Skip to content

Instantly share code, notes, and snippets.

@jjmalina
Created November 20, 2012 04:35
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 jjmalina/4116035 to your computer and use it in GitHub Desktop.
Save jjmalina/4116035 to your computer and use it in GitHub Desktop.
Columns
def columns(size, columns=30):
"""
Return the number of items in each of X columns given a number.
Basically the idea is to find exact or next largest rectangle of a given
number and given amount of columns. Then fill each column evenly.
"""
nearest_rect = 0
column_items = [0 for i in xrange(columns)]
for index in xrange(columns):
if size % columns == 0:
nearest_rect = size
else:
nearest_rect = (size // columns) * columns + columns
if index == 0:
items = nearest_rect / columns
else:
items = nearest_rect // columns
column_items[index] = items
size -= items
columns -= 1
return column_items
if __name__ == '__main__':
for i in range(300):
print columns(i)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment