Terrain Rendering

Asked at AirbnbPhone screen

Reported by the problem's creator, not verified

You are drawing a side view of terrain made of columns. heights[i] is the height of column i, a whole number of cells.

Write render_terrain(heights), which returns the picture as a list of text lines, from the top line to the bottom one. The picture has one character per column per line. A column of height h is + in its lowest h lines above the base, and a blank everywhere else. The last line is the base: one + for every column. The first line is the line of the tallest column, so a picture has max(heights) lines above the base. Lines have no trailing spaces, but spaces on the left are kept. If every height is 0, the picture is only the base line.

Examples

  1. Example 1

    Input: heights = [5,4,3,2,1,3,4,0,3,4]

    Output: ["+","++ + +","+++ ++ ++","++++ ++ ++","+++++++ ++","++++++++++"]

    Explanation: Each column is + for as many rows as its height, and the last line is the base. Picture: ``` + ++ + + +++ ++ ++ ++++ ++ ++ +++++++ ++ ++++++++++ ```

Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question

Code

Loading editor…

Ln 1, Col 1

Start an attempt to use Coach.