The shelf that settles
- O(n) · Medium
- Full plan
- Python
- JavaScript
- grids
- strings
- indexes
Problem
Nayeli tidies up her stationery shop before closing. On the shelf, each slot either holds a box or is empty, and she wants to see how everything looks once the boxes go all the way down: no box changes column, each one only drops until it hits the bottom of the shelf or another box.
Write a function that takes shelf, a list of strings of the same length: the first string is the top row and the last one the bottom row, # is a box and . an empty slot. The shelf is at most 8 rows by 8 slots, and it may come in empty or with empty rows.
Return a new list of strings, the same size, with the boxes already fallen.
With ["#.#", "..#", ".#."] the first column holds one box and the second another one, and both end up in the bottom row; the third holds two, which end up in the two bottom rows. You return ["...", "..#", "###"].
Examples
The example
["#.#", "..#", ".#."] → ["...", "..#", "###"]
The shelf with no boxes
["...", "...", "..."] → ["...", "...", "..."]
The full shelf
["###", "###", "###"] → ["###", "###", "###"]
It was already settled
["...", "..#", "###"] → ["...", "..#", "###"]
A single box at the very top
["#..", "...", "..."] → ["...", "...", "#.."]
A shelf of a single row
["#.#"] → ["#.#"]
A shelf of a single column
["#", ".", "#", "."] → [".", ".", "#", "#"]
Two boxes apart in the same column
["#..", "...", "#..", ".#."] → ["...", "...", "#..", "##."]
Besides these, the challenge has hidden tests that are revealed when you submit your solution.
You start with this
Python
def settle_shelf(shelf):
passJavaScript
function settleShelf(shelf) {
}It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.