Created
November 20, 2012 04:33
-
-
Save lsemel/4116020 to your computer and use it in GitHub Desktop.
Break content into columns
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
from math import floor | |
indexes_of_column_breaks = {} | |
def column(current_count, total_count, num_cols): | |
cols = indexes_of_column_breaks.get((total_count, num_cols), []) | |
if not cols: | |
# Compute the indexes where a new column will start | |
cols.append(0) | |
# The minimum number of items per column | |
base = int(floor(total_count / num_cols)) | |
for i in xrange(0, num_cols): | |
cols.append(base) | |
# Add in the extras, left to right | |
for j in xrange(0, total_count % num_cols): | |
cols[j+1] += 1 | |
# New we have the total numbers. Add them up to convert them to indexes | |
for i, pos in enumerate(cols): | |
if i > 0: | |
cols[i] += cols[i-1] | |
# Save it so it need not be recomputed | |
indexes_of_column_breaks[(total_count, num_cols)] = cols | |
if current_count in cols: | |
if current_count > 0: | |
print 'end column' | |
print 'start column' | |
def test_column(count, num_cols): | |
for i in xrange(0, count): | |
column(i, count, num_cols) | |
print i | |
for j in xrange(1,7): | |
for i in xrange(0, 20): | |
print 'with %s items in %s columns' % (i, j) | |
test_column(i, j) | |
print "\n-------------------------" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment