-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathText_Justification.py
More file actions
66 lines (53 loc) · 1.37 KB
/
Text_Justification.py
File metadata and controls
66 lines (53 loc) · 1.37 KB
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
59
60
61
62
63
64
65
66
# -*- coding:utf-8 -*-
import sys
class Solution(object):
def fullJustify(self, words, maxWidth):
n = maxWidth
sentence_list = []
ret_list = []
sentence_l = 0
word_c = 0
for word in words:
word_l = len(word)
word_c += 1
if sentence_l + word_l + word_c - 1 <= n:
sentence_l += word_l
sentence_list.append(word)
else:
word_c -= 1
if word_c > 1:
space = (n - sentence_l) / (word_c - 1)
space_surplus = (n - sentence_l) % (word_c - 1)
s = ""
if word_c == 1:
s = s + sentence_list[0] + " " * (n - len(sentence_list[0]))
else:
for s_word in sentence_list:
if word_c > 1:
s = s + s_word + " " * (space + (lambda x:1 if x > 0 else 0)(space_surplus))
space_surplus -= 1
else:
s = s + s_word
word_c -= 1
sentence_l = len(word)
ret_list.append(s)
sentence_list = [word]
word_c = 1
if word_c > 1:
space = (n - sentence_l) / (word_c - 1)
space_surplus = (n - sentence_l) % (word_c - 1)
s = ""
q = word_c - 1
for s_word in sentence_list:
if word_c > 1:
s = s + s_word + " "
else:
s = s + s_word + " " * (n - sentence_l - q)
word_c -= 1
ret_list.append(s)
return ret_list
if __name__ == "__main__":
s = Solution()
words = ["This", "is", "an", "example", "of", "text", "justification."]
maxWidth = 16
print s.fullJustify(words, maxWidth)