Print Sentences as a Table

Asked at AirbnbPhone screen

Reported by the problem's creator, not verified

You are printing a list of sentences as a bordered text table. Each row holds width characters of text between the borders, and every sentence goes on a single row.

Write print_table(sentences, width) and return the table as a list of lines, one string per line.

Examples

  1. Example 1

    Input: sentences = ["Hello world","How are you today","Bye"], width = 55

    Output: ["+---------------------------------------------------------+","| Hello world |","+---------------------------------------------------------+","| How are you today |","+---------------------------------------------------------+","| Bye |","+---------------------------------------------------------+"]

    Explanation: Each row is |, a space, the sentence padded with spaces on the right to 55 characters, a space and |, so every row is 59 characters. Each rule is +, 57 dashes and +. One rule sits between neighbouring rows, so three sentences give seven lines.

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.