Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Watch + case solution in Creative category for Text Formatting by U.V
def text_formatting(text: str, width: int, style: str) -> str:
words = text + ' '
ans = []
while words:
if ' ' in words:
i = words.rindex(' ', 0,width+1)
else:
ans.append(words)
break
line = words[:i].strip()
words = words[i:].lstrip()
match style:
case 'l':
pass
case 'c':
line = line.center(width).rstrip()
case 'r':
line = line.rjust(width)
case 'j':
w = line.split()
wc = line.count(' ')
if wc:
adsp = width - len(line) + wc
n = adsp // wc
for i in range(adsp-n*wc):
w[i] = w[i] + ' '
line = (' ' * n).join(w)
ans.append(line)
if style == 'j':
ans[-1] = ' '.join(ans[-1].split())
a1 = '\n'.join(ans)
return a1
Sept. 3, 2022
Comments: