-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtextJusification.py
58 lines (46 loc) · 1.5 KB
/
textJusification.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
48
49
50
51
52
53
54
55
56
57
58
"""
iterate through words till it exceeds len(L):
remove last word. if len(L) > len(words) then
create the difference to spaces. Iterate through
spaces (every odd index) and add one till no more
spaces.
append that to the master list and repeat
if last line or len(words) == 1, we need ot left justifiy.
append the word and append spaces len(L) - len(words)
"""
def distributeSpaces(words, spaces):
numEachSpaces = (2/spaces)
for i in range(1,len(words)-2,2):
words[i] = words[i] + numEachSpaces*' '
def justifityLeft(words, spaces):
return words + " "*spaces
def textJustified(words, L):
master = []
cur = []
counter = L
for i in range(0,len(words)-1):
word = words[i]
if len(word) <= counter:
cur.append(word)
cur.append(' ')
# need to account for the space
counter = counter - (len(word)+1)
else:
spaces = counter
del cur[-1]
if len(cur) > 1:
distributeSpaces(cur, spaces+1)
else:
cur = justifyLeft(cur, spaces)
master.append(''.join(cur))
cur = [word, ' ']
print cur
counter = L - (len(word)+1)
print counter
lastWord = words[-1]
justifityLeft(lastWord, L-len(lastWord[0]))
master.append(lastWord)
return master
words = ["This", "is", "an", "example", "of", "text", "justification."]
L = 16
print textJustified(words,L)