Text Justification


In [140]:
import sys; sys.path.append('../..')
from puzzles import leet_puzzle
leet_puzzle('text-justification')


Source : https://leetcode.com/problems/text-justification

Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified.

You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces ' ' when necessary so that each line has exactly L characters.

Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line do not divide evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right.

For the last line of text, it should be left justified and no extra space is inserted between words.

For example,
words: ["This", "is", "an", "example", "of", "text", "justification."]
L: 16.

Return the formatted lines as:

[
   "This    is    an",
   "example  of text",
   "justification.  "
]

Note: Each word is guaranteed not to exceed L in length.

click to show corner cases.

Corner Cases:

  • A line other than the last line might contain only one word. What should you do in this case?
    In this case, that line should be left-justified.


In [136]:
def justify_line(line, current_length, length):

    if len(line) > 1:
        # calculate the number of extra spaces required per word
        extra_spaces = length - current_length + (len(line) - 1)
        extra_spaces_per_word = extra_spaces / (len(line) - 1)
        sep = ' '  * extra_spaces_per_word

        # if the number of spaces doesn't divide evenly the left most slots will
        # be assigne dmore spaces than the slots on the right.
        extra_left_spaces = extra_spaces - (extra_spaces_per_word * (len(line) - 1))
        split_point = len(line) - extra_left_spaces
        if extra_left_spaces:
            split_point = extra_left_spaces + 1
            return sep.join([
                (sep + ' ').join(line[:split_point]),
                sep.join(line[split_point:]),
            ])
        else:
            return sep.join(line)
    else:
        # left justify when only one word is present
        return line

    
def get_new_line_length(current_length, word):
    # +1 if not first word since we will need at least one space.
    return current_length + len(word) + (1 if current_length else 0)

    
def justify_words(words, length):
    
    line, lines, current_length = [], [], 0
    
    for word in words:
        new_length = get_new_line_length(current_length, word)
        if new_length > length:
            line = justify_line(line, current_length, length)
            lines.append(line)
            line, current_length = [word], len(word)
        else:
            current_length = get_new_line_length(current_length, word)
            line.append(word)
            
    return lines + [line[0]]

In [138]:
import json
    
result = justify_words(["This", "is", "an", "example", "of", "text", "justification."], 16)  
print json.dumps(result, indent=4)    

text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
result = justify_words(text.split(' '), 79)
print json.dumps(result, indent=4)


[
    "This    is    an", 
    "example  of text", 
    "justification."
]
[
    "Lorem  ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor", 
    "incididunt  ut  labore  et  dolore  magna aliqua. Ut enim ad minim veniam, quis", 
    "nostrud  exercitation  ullamco laboris nisi ut aliquip ex ea commodo consequat.", 
    "Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu", 
    "fugiat  nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in", 
    "culpa"
]