Q1. We are building a word processor and we would like to implement a "word-wrap" functionality.
Given a list of words followed by a maximum number of characters in a line, return a collection of strings where each string element represents a line that contains as many words as possible, with the words in each line being concatenated with a single '-' (representing a space, but easier to see for testing). The length of each string must not exceed the maximum character length per line.
Your function should take in the maximum characters per line and return a data structure representing all lines in the indicated max length.
Examples:
words1 = [ "The", "day", "began", "as", "still", "as", "the",
"night", "abruptly", "lighted", "with", "brilliant",
"flame" ]
wrapLines(words1, 13) "wrap words1 to line length 13" =>
[ "The-day-began",
"as-still-as",
"the-night",
"abruptly",
"lighted-with",
"brilliant",
"flame" ]
wrapLines(words1, 20) "wrap words1 to line length 20" =>
[ "The-day-began-as",
"still-as-the-night",
"abruptly-lighted",
"with-brilliant-flame" ]
words2 = [ "Hello" ]
wrapLines(words2, 5) "wrap words2 to line length 5" =>
[ "Hello" ]
words3 = [ "Hello", "world" ]
wrapLines(words3, 5) "wrap words3 to line length 5" =>
[ "Hello",
"world" ]
words4 = ["Well", "Hello", "world" ]
wrapLines(words4, 5) "wrap words4 to line length 5" =>
[ "Well",
"Hello",
"world" ]
words5 = ["Hello", "HelloWorld", "Hello", "Hello"]
wrapLines(words5, 20) "wrap words 5 to line length 20 =>
[ "Hello-HelloWorld",
"Hello-Hello" ]
All Test Cases:
words, max line length
wrapLines(words1, 13)
wrapLines(words1, 20)
wrapLines(words2, 5)
wrapLines(words3, 5)
wrapLines(words4, 5)
wrapLines(words5, 20)
n = number of words OR total characters
Q2. We are building a word processor and we would like to implement a "reflow" functionality that also applies full justification to the text.
Given an array containing lines of text and a new maximum width, re-flow the text to fit the new width. Each line should have the exact specified width. If any line is too short, insert '-' (as stand-ins for spaces) between words as equally as possible until it fits.
Note: we are using '-' instead of spaces between words to make testing and visual verification of the results easier.
lines = [ "The day began as still as the",
"night abruptly lighted with",
"brilliant flame" ]
reflowAndJustify(lines, 24) "reflow lines and justify to length 24" =>
[ "The--day--began-as-still",
"as--the--night--abruptly",
"lighted--with--brilliant",
"flame" ] // <--- a single word on a line is not padded with spaces
reflowAndJustify(lines, 25) "reflow lines and justify to length 25" =>
[ "The-day-began-as-still-as"
"the-----night----abruptly"
"lighted---with--brilliant"
"flame" ]
reflowAndJustify(lines, 26) "reflow lines and justify to length 26" =>
[ "The--day-began-as-still-as",
"the-night-abruptly-lighted",
"with----brilliant----flame" ]
reflowAndJustify(lines, 40) "reflow lines and justify to length 40" =>
[ "The--day--began--as--still--as-the-night",
"abruptly--lighted--with--brilliant-flame" ]
reflowAndJustify(lines, 14) "reflow lines and justify to length 14" =>
['The--day-began',
'as---still--as',
'the------night',
'abruptly',
'lighted---with',
'brilliant',
'flame']
All Test Cases:
lines, reflow width
reflowAndJustify(lines, 24)
reflowAndJustify(lines, 25)
reflowAndJustify(lines, 26)
reflowAndJustify(lines, 40)
reflowAndJustify(lines, 14)
n = number of words OR total characters