runtime
60 / 500
question 60 Strings & Text · hard

Wrap a complaint issue to fit a screen

A narrow display shows CFPB complaint issues, such as Company closed your account without telling you first, on lines of at most width characters. Wrap the text greedily: put as many whole words on a line as fit (with one space between words), and start a new line when the next word would make it too long. Words are separated by single spaces, and no word is longer than width. Lists come in the next module, so build each word and each line as text while you walk the characters.

Inputstext (words separated by single spaces) and width (a whole number at least as long as the longest word)
Printseach wrapped line on its own line, with no spaces at the start or end of a line
Exampleswidth 20: Company closed your / account without / telling you first
Expected output3 lines
  1. 1Company closed your
  2. 2account without
  3. 3telling you first

Loading your progress…

Wrap a complaint issue to fit a screen — Python practice · Runtime