Comcast | Eng 1, Software Dev & Engineering | OA
Anonymous User
2547
Jul 18, 2020

Text Wrap Problem

Write a program that takes an input string and prints it as multiple lines of text such that no line of text is greater than 13 characters and words are kept whole.

For example, the first line of the Gettysburg address:
Four score and seven years ago our fathers brought forth upon this continent a new nation, conceived in liberty and dedicated to the proposition that all men are created equal

Becomes:
Four score
and seven
years ago our
fathers
brought
forth upon
this
continent a
new nation,
conceived in
liberty and
dedicated to
the
proposition
that all men
are created
equal

NOTE: The formatting of above output excerpt isn't proper. It needs to be justified.


Identical to LC #68. Text Justification
The only difference is the input and output type. (In the 'Text Wrapper Problem', the input and output types are 'str' and not 'List[str]').


Solution:

class Solution:
       def text_wrap(self, words: str, max_width: int) -> str:
        if not words:
            return ''

        words = list(words.split())  # str -> List[str]
        # result -> list of sentences, curr -> list of words in current sentence, n_chars -> current character count
        result, curr, n_chars = [], [], 0

        for w in words:
            if n_chars + len(curr) + len(w) > max_width:
                # 1 word left
                if len(curr) == 1:
                    result.append(curr[0] + ' ' * (max_width - n_chars))
                else:
                    num_spaces = max_width - n_chars
                    # '-1' below indicates that the last word in 'curr' doesn't need a space after it
                    spaces_bw_words, extra_spaces = divmod(num_spaces, len(curr) - 1)

                    # Taking care of extra spaces first in a 'Round Robin' manner
                    for i in range(extra_spaces):
                        curr[i] += ' '
                    # Taking care of the spaces between words
                    result.append((' ' * spaces_bw_words).join(curr))
                # Reinitialize for the next sentence
                curr, n_chars = [], 0
            # If 'w' has to be appended in the same sentence ->. no overflow yet
            curr.append(w)
            n_chars += len(w)
        # Handling teh left over words
        # n(padding) to be inserted -> number of spaces left
        result.append(' '.join(curr) + ' ' * (max_width - n_chars - len(curr) + 1))
        return '\n'.join(result)
Comments (1)