-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path68.py
38 lines (36 loc) · 1.31 KB
/
68.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
class Solution(object):
def fullJustify(self, words, maxWidth):
"""
:type words: List[str]
:type maxWidth: int
:rtype: List[str]
"""
ret = []
cur = []
for i, word in enumerate(words):
if len(' '.join(cur))+len(word)<=maxWidth-1:
cur.append(word)
else:
# print cur
spaces = maxWidth-len(''.join(cur))-len(cur)+1
if len(cur)>0:
if len(cur) > 1:
a, b = divmod(spaces, len(cur)-1)
for i in range(len(cur)-1):
cur[i] += ' '*a
if b > 0:
cur[i] += ' '
b -= 1
ret.append(' '.join(cur))
else:
ret.append(cur[0]+' '*(maxWidth-len(cur[0])))
cur = [word]
s = ' '.join(cur)
ret.append(s+' '*(maxWidth-len(s)))
# for i in ret:
# print i, len(i)
return ret
if __name__ == '__main__':
solution = Solution()
print solution.fullJustify(["Science","is","what","we","understand","well","enough","to","explain","to","a","computer.","Art","is","everything","else","we","do"]
,20)