here2share

# balanced_columns_demo.py

Mar 4th, 2026
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.21 KB | None | 0 0
  1. # balanced_columns_demo.py
  2.  
  3. text_height = 18
  4. button_height = 32
  5. additional_ypadding = 12
  6.  
  7. data = [
  8.     ("Galleries", 5),
  9.     ("Cultural Centres", 5),
  10.     ("Performances", 4),
  11.     ("Workshops", 4),
  12.     ("Heritage Sites", 3),
  13.     ("Public Art", 3),
  14.     ("Lectures", 2),
  15.     ("Film Screenings", 2),
  16.     ("Pubs", 4),
  17.     ("Lounges", 3),
  18.     ("Patios", 4),
  19.     ("Billiards", 3),
  20.     ("Coffeehouses", 3),
  21.     ("Wine Bars", 2),
  22.     ("Live Music", 3),
  23.     ("Trivia Nights", 2),
  24.     ("Walking Trails", 5),
  25.     ("Golf", 3),
  26. ]
  27.  
  28. blocks = []
  29. for name, rows in data:
  30.     px_height = rows * text_height + button_height + additional_ypadding
  31.     blocks.append((name, px_height))
  32.  
  33. column_count = 3  # or 4 or 5
  34. columns = [[] for _ in range(column_count)]
  35. heights = [0] * column_count
  36.  
  37. blocks_sorted = sorted(blocks, key=lambda x: x[1], reverse=True)
  38.  
  39. for name, h in blocks_sorted:
  40.     idx = heights.index(min(heights))
  41.     columns[idx].append((name, h))
  42.     heights[idx] += h
  43.  
  44. print("Column heights:", heights)
  45. print("Imbalance:", max(heights) - min(heights))
  46. print()
  47.  
  48. for i, col in enumerate(columns):
  49.     print(f"Column {i+1}:")
  50.     for name, h in col:
  51.         print(f"  {name} ({h}px)")
  52.     print()
Advertisement
Add Comment
Please, Sign In to add comment