KatInterview

Boxed Multi-Width Paragraph Formatter

StringsAuto-graded · 23 test cases
MediumAuto-gradedStringsstringssimulationgreedy

Last updated

Greedily word-wrap each paragraph to its own width, then frame all resulting lines in one padded ASCII box sized by the widest paragraph limit.

Boxed Multi-Width Paragraph Formatter

A command-line tool needs to print several blocks of text inside one bordered frame. Each block comes with its own line-length limit, and the frame must be wide enough for the largest limit.

You are given a list blocks. Each element is a pair (text, limit): a string of words and the maximum number of characters allowed on a line for that block.

Task

  1. Wrap each block. Process the words of text from left to right and build lines greedily. Put each word on the current line if the line, with a single space and that word added, still has at most limit characters. Otherwise, close the current line and begin a new one with that word. Words are never split. Words in an output line are separated by exactly one space, regardless of the spacing in the input. A block with no words produces no lines.
  2. Choose the inner width. Let W be the largest limit among all blocks, including blocks that produce no lines.
  3. Build the frame. Return the rows from top to bottom:
    • A border row: +, then W + 2 hyphens (-), then +.
    • One row for every wrapped line, in block order and then line order within the block: "| ", then the line left-aligned and padded on the right with spaces to exactly W characters, then " |".
    • The same border row again.

If no block produces any lines, the result is just the two border rows.

Function

Implement render_boxed_blocks(blocks), which returns the list of row strings.

Constraints

  • 1 <= len(blocks) <= 100
  • 1 <= limit <= 200 for every block
  • text consists of ASCII words separated by one or more spaces. It may have leading or trailing spaces, and it may be empty or contain only spaces.
  • Every word in a block has length at most that block's limit.
  • Each block's lines are padded to W, the global maximum, not to that block's own limit.

Examples

Example 1

Input: render_boxed_blocks([['Hello world from Airbnb', 12], ['Enjoy your stay in our cozy apartment', 16]])

Output: ['+------------------+', '| Hello world |', '| from Airbnb |', '| Enjoy your stay |', '| in our cozy |', '| apartment |', '+------------------+']

Example 2

Input: render_boxed_blocks([['A quick brown fox', 10]])

Output: ['+------------+', '| A quick |', '| brown fox |', '+------------+']

Example 3

Input: render_boxed_blocks([['One two three', 7], ['alpha beta', 11], ['', 5]])

Output: ['+-------------+', '| One two |', '| three |', '| alpha beta |', '+-------------+']

Hints

Hint 1

Split each text on whitespace with no separator argument so repeated, leading and trailing spaces vanish and blank text gives an empty word list.

Hint 2

Keep a running length for the current line. A word fits if current_length + 1 + len(word) <= limit; an empty line just takes the word.

Hint 3

Compute W as the maximum limit over every block before rendering, even for blocks that contribute no lines, then pad each line to W and use W + 2 hyphens in the border.