The snail's shell
- O(n²) · Very hard
- Full plan
- Python
- JavaScript
- strings
- loops
- indexes
Problem
A cone snail's shell grows at its edge, one line of pigment at a time: each new cell is painted according to its neighbors in the line before, and that is how the triangles on it appear. Leilani Kahale, a malacologist, wants to predict the edge after several generations.
Write a function that takes row, a string of # (painted cell) and . (unpainted) with at least one character, and k, a whole number from 0 up. In each generation every cell changes at the same time, looking at the previous row: a cell becomes # if exactly one of its two neighbors (the one on the left or the one on the right) was #, and . if both were or neither was; its own state does not matter. Beyond the edges everything counts as .. Return the row, with the same length, after k generations; when k is 0, return the row as it is.
With "...#..." and 3: the first generation gives "..#.#..", the second ".#...#." (the center had # on both sides) and the third "#.#.#.#", so it returns "#.#.#.#".
Examples
The example
"...#...", 3 → "#.#.#.#"
Zero generations
"..#..", 0 → "..#.."
A single cell goes blank
"#", 1 → "."
The whole row painted
"#####", 1 → "#...#"
It starts at the edge
"#....", 2 → "#.#.."
Besides these, the challenge has hidden tests that are revealed when you submit your solution.
You start with this
Python
def paint_edge(row, k):
passJavaScript
function paintEdge(row, k) {
}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.