http://qs1969.pair.com?node_id=180309


in reply to Puzzle: need a more general algorithm

To distribute N categories across M columns as evenly as possible, assuming N >= M, observe that

  1. (N mod M) columns will contain ((N div M) + 1) categories
  2. the remaining (M - (N mod M)) columns, if any, will contain (N div M) categories.

Given this, categories can be distributed in one pass.

Update: Oh blast. I may have misread the problem, and confused the "height of the table in rows" with "the aggregate height summed from the @height data".