-
Notifications
You must be signed in to change notification settings - Fork 0
/
textJustification.py
47 lines (36 loc) · 1.55 KB
/
textJustification.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
__author__ = 'kathan'
def fullJustify(words, maxWidth):
output_list = []
word_ptr = 0
while word_ptr < len(words):
len_so_far = 0
words_so_far = []
while len_so_far < maxWidth and word_ptr < len(words):
temp_length = len_so_far + len(words[word_ptr])
if temp_length > maxWidth:
break
words_so_far.append(words[word_ptr])
len_so_far = len_so_far + len(words[word_ptr]) + 1
word_ptr += 1
spaces_array = []
if len(words_so_far) > 1:
len_diff = maxWidth - (len_so_far - len(words_so_far))
num_of_space_after_each_word = len_diff / (len(words_so_far) - 1)
remaining_spaces = len_diff % (len(words_so_far) - 1)
spaces_array = [num_of_space_after_each_word] * (len(words_so_far) - 1)
if remaining_spaces != 0:
for i in range(remaining_spaces):
spaces_array[i] += 1
line_formation = ""
for index, word in enumerate(words_so_far):
if index < len(spaces_array):
line_formation += word + (' ' * spaces_array[index])
else:
line_formation += word + (' ' * (maxWidth - len(line_formation + word)))
output_list.append(line_formation)
if line_formation == "":
break
return output_list
print fullJustify(["This", "is", "an", "example", "of", "text", "justification."], 16)
print fullJustify(["What", "must", "be", "shall", "be."], 12)
print fullJustify([""], 0)