You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Given an array of words and a max_width parameter, write a function justify to format the text such that each line has exactly max_width characters. Pad extra spaces ’ ‘ when necessary so that each line has exactly max_width characters.
4
+
5
+
Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line does not divide evenly between words, place excess spaces on the right-hand side of each line.
6
+
7
+
Note: You may assume that there is no word in words that is longer than max_width.
8
+
'''
9
+
10
+
11
+
defjustify(words, max_width):
12
+
13
+
res= []
14
+
cur= []
15
+
num_of_letters=0
16
+
17
+
forwinwords:
18
+
ifnum_of_letters+len(w) +len(cur) >max_width:
19
+
foriinrange(max_width-num_of_letters):
20
+
cur[i% (len(cur) -1or1)] +=' '
21
+
res.append(''.join(cur))
22
+
cur, num_of_letters= [], 0
23
+
cur+= [w]
24
+
num_of_letters+=len(w)
25
+
26
+
# Treat the last line in the same way as the other lines
0 commit comments