In [140]:
import sys; sys.path.append('../..')
from puzzles import leet_puzzle
leet_puzzle('text-justification')
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)