-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWWchallenge.py
More file actions
27 lines (22 loc) · 803 Bytes
/
WWchallenge.py
File metadata and controls
27 lines (22 loc) · 803 Bytes
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
def add_whitespace(words, line_char_limit):
wordsArray = words.split()
res = []
string = []
currentLength = 0
for word in wordsArray:
# Check if adding the current word to the current line exceeds the line length
if currentLength + len(string) + len(word) <= line_char_limit:
string.append(word)
currentLength += len(word)
else:
res.append(' '.join(string))
string = [word]
currentLength = len(word)
#balance the whitespaces
if string:
res.append(' '.join(string))
for i in range(len(res)):
if len(res[i]) < line_char_limit:
res[i] += " " * (line_char_limit - len(res[i]))
return res
print(add_whitespace("a cat is an animal", 6))