-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtextJustification.py
More file actions
34 lines (27 loc) · 1.17 KB
/
Copy pathtextJustification.py
File metadata and controls
34 lines (27 loc) · 1.17 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
from typing import List
class Solution:
def split_number(self, n: int, parts: int) -> List[int]:
base = n // parts
remainder = n % parts
return [base + 1] * remainder + [base] * (parts - remainder)
def rowJustify(self, words: List[str], maxwidth: int, chars: int, leftJustification=False) -> str:
if len(words) == 1 or leftJustification:
line = " ".join(words)
return line + " " * (maxwidth - len(line))
spaces = self.split_number(maxwidth - chars, len(words) - 1) + [0]
return "".join([word + " " * space for word, space in zip(words, spaces)])
def fullJustify(self, words: List[str], maxwidth: int) -> List[str]:
result = []
temp = []
chars = 0
for word in words:
if chars + len(word) + len(temp) <= maxwidth:
chars += len(word)
temp.append(word)
else:
result.append(self.rowJustify(temp, maxwidth, chars))
chars = len(word)
temp = [word]
if temp:
result.append(self.rowJustify(temp, maxwidth, chars, leftJustification=True))
return result