Gravity and falling pieces
In a falling-pieces problem each column is independent. Process one column, bottom to top, and remember the lowest free row, the floor.
floor = rows - 1 for r in range(rows - 1, -1, -1): if cell is a piece: put it at row floor of the new grid floor -= 1 elif cell is a fixed wall: floor = r - 1 # pieces above rest on it
Build the answer in a new grid of empty cells so you never read cells you already moved. Rules to check in each statement: what falls, what blocks, whether pieces keep their order, and the output type (a list of strings or a list of lists).
The slow way is to move every piece one step, over and over, until nothing changes. That is up to R passes over the whole grid. One pass per column is enough.
Watch the visual: one crate drops until the cell below is not empty.
See it run
grid = [["C", "."], [".", "C"], [".", "."]] r = 0 while r + 1 < 3 and grid[r + 1][0] == ".": grid[r][0], grid[r + 1][0] = grid[r + 1][0], grid[r][0] r += 1 print(grid)
The crate in column 0 sits at the top. Column 1 has its own crate.
Practice gravity and falling pieces
Exercise 1
A skyline is a list of stack heights: heights[i] is how many blocks are stacked in column i. A flat piece that is w blocks wide and 1 block high is dropped from above, with its left end over column x, so it covers columns x to x + w − 1. It falls straight down until it rests on the tallest of the covered columns.
Return the new list of heights after the piece has landed.
Rules:
- The piece always fits:
x + w ≤ len(heights). - After landing, every covered column has height (tallest covered column before) + 1. This can leave gaps under the piece, which are ignored.
- Columns that are not covered keep their height.
Constraints: 1 ≤ len(heights) ≤ 100; 0 ≤ height ≤ 100.
Example: solution([2, 0, 1, 3], 0, 3) returns [3, 3, 3, 3]: the covered columns are 2, 0, 1; the tallest is 2, so all three become 3.
Exercise 2
A warehouse side view is a list of equal-length strings, row 0 at the top. "." is empty space and "C" is a crate. Gravity pulls every crate straight down, in its own column, until it rests on the floor (the last row) or on another crate.
Return the view after everything has settled, as a list of strings of the same size.
Rules:
- Crates never move sideways, and every column keeps the same number of crates.
- Do not change the input list.
Constraints: 1 ≤ rows, columns ≤ 100.
Example: solution(["CC", "..", "C."]) returns ["..", "C.", "CC"]: in column 0 the crates at rows 0 and 2 end at rows 1 and 2; the column 1 crate falls to row 2.
Exercise 3
A warehouse side view is a list of equal-length strings, row 0 at the top: "." is empty, "C" is a crate and "=" is a fixed shelf. Gravity pulls every crate straight down in its own column until it rests on the floor (the last row), on a shelf, or on another crate.
Return the view after everything has settled, as a list of strings of the same size.
Rules:
- Shelves never move, and a crate can never pass through or move onto a shelf, so crates above a shelf settle on top of it.
- Crates already resting stay where they are. Crates never move sideways.
- Do not change the input list.
Constraints: 1 ≤ rows, columns ≤ 400. A large view must be settled quickly: do not simulate one step at a time.
Example: solution(["C.", "..", "=.", "C."]) returns ["..", "C.", "=.", "C."]: the top crate falls one row and lands on the shelf; the bottom crate is already on the floor.
The explanation and tasks are public. Running this lesson requires an account with course access.
Practice
Loading Python…Visualizer Watch your code run line by line
Run your code, then choose Play to follow its execution.