Hackbright Code Challenges

Fit String to Width: Solution

Fit String to Width: Solution

Problem

Fit String to Width

Whiteboard

Medium

Challenge

Easier

Concepts

Loops

Download

fit-to-width-solution.zip


def fit_to_width(string, limit):
    """Print string within a character limit."""

    # START SOLUTION

    # Create a stack of tokens
    tokens = list(reversed(string.split()))
    lines = []

    curr_line = []
    while tokens:
        # Test to see if current word fits in the line
        word = tokens[-1]
        if len(' '.join(curr_line + [word])) <= limit:
            curr_line.append(tokens.pop())

        # If the word doesn't fit, start a new line
        else:
            if curr_line:
                lines.append(' '.join(curr_line))
            curr_line = []

    # Append remaining line
    lines.append(' '.join(curr_line))

    for line in lines:
        print(line)