-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path68.py
More file actions
86 lines (50 loc) · 1.93 KB
/
68.py
File metadata and controls
86 lines (50 loc) · 1.93 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from typing import List
class Solution:
def fullJustify(self, words: List[str], maxWidth: int) -> List[str]:
# Split words into a 2d array
# Where the total length of each array should be less than maxWidth
# There should be a space appended after every entry, so it's len(word) + 1 for each entry
# Come back to spacing between words
result = []
temp = []
len_temp = 0
words_q = words[:]
while words_q:
curr_word = words_q.pop(0)
if len_temp + len(curr_word) > maxWidth:
result.append(temp)
temp = [curr_word]
len_temp = len(curr_word)+1
else:
temp.append(curr_word)
len_temp += len(curr_word) + 1
result.append(temp)
ctr = 0
for row in result[:-1]:
res = sum([len(i) for i in row])
prev_len = len(row)
num_spaces = maxWidth - res
pos = 1
if len(row) - 1 == 0:
row.insert(pos, " "*num_spaces)
else:
curr_spaces = (num_spaces//(prev_len-1)) + (num_spaces % (prev_len-1))
while num_spaces > 0:
row.insert(pos, " " * curr_spaces)
pos += 2
num_spaces -= curr_spaces
# For the last line
# print()
last_space = (maxWidth - sum([len(i) for i in result[-1]]))
pos = 1
if len(result[-1]) > 1:
for i in range(len(result[-1][:-1])):
result[-1].insert(pos, " ")
last_space -= 1
result[-1].append(" " * last_space)
# result[-1].append(last_space)
for i in range(len(result)):
result[i] = "".join(result[i])
return result
s = Solution()
print(s.fullJustify(["This", "is", "an", "example", "of", "text", "justification."], 16))