- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext_format.py
35 lines (29 loc) · 713 Bytes
/
text_format.py
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
defformat_text(text):
words=text.replace(',', ', ').split()
i=0
whileTrue:
try:
i=words.index(',', i)
words[i-1] +=words.pop(i)
exceptValueError:
break
max_word_length=max(len(word.rstrip(',')) forwordinwords)
max_line_length=max_word_length*3
lines= []
cur_line=''
forwordinwords:
iflen(cur_line) +len(word) + (bool(cur_line)) <=max_line_length:
ifcur_line:
cur_line+=' '
cur_line+=word
else:
lines.append(cur_line)
cur_line=word
ifcur_line:
lines.append(cur_line)
return'\n'.join(lines)
if__name__=='__main__':
importsys
input=sys.stdin.read().strip()
formatted_text=format_text(input)
print(formatted_text)