Data Visualization: Lay out panels on a 12-column grid

Dashboard tools place panels left to right on a 12-column grid and wrap to a new row when the next panel does not fit. Compute each panel's position.

What to build

A program that places dashboard panels on a 12-column grid.

Requirements

  • Place the panels in order. A panel of width w starts at the next free column of the current row if it fits within column 12; otherwise it starts at column 1 of a new row.
  • Print NAME: row R, columns A-B for each panel, with rows and columns counted from 1.

Input

Line 1: n. Then n lines NAME WIDTH.

Output

n lines.

Constraints

1 ≤ n ≤ 50; 1 ≤ WIDTH ≤ 12.

Example

Input:

5
Revenue 4
Orders 4
Visitors 4
Chart 8
Table 4

Output:

Revenue: row 1, columns 1-4
Orders: row 1, columns 5-8
Visitors: row 1, columns 9-12
Chart: row 2, columns 1-8
Table: row 2, columns 9-12

Why it works

Tracking the next free column of the current row is exactly how flowing layouts work: a panel either fits in the space left or wraps, so every row is filled from the left and no panel crosses the grid's edge.

Examples

Example 1 · Input
5
Revenue 4
Orders 4
Visitors 4
Chart 8
Table 4
Expected output
Revenue: row 1, columns 1-4
Orders: row 1, columns 5-8
Visitors: row 1, columns 9-12
Chart: row 2, columns 1-8
Table: row 2, columns 9-12
Example 2 · Input
1
Big 12
Expected output
Big: row 1, columns 1-12

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

Keep the next free column of the current row, starting at 1.

Hint 2

If a panel does not fit in the columns left, start a new row at column 1.

Solution

Show one correct solution (try it yourself first)
n = int(input())
row, col = 1, 1
for _ in range(n):
    name, w = input().split()
    w = int(w)
    if col + w - 1 > 12:
        row, col = row + 1, 1
    print(f"{name}: row {row}, columns {col}-{col + w - 1}")
    col += w

Lesson: Designing dashboards →