Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
13-liner: justify yourself solution in Clear category for Text Formatting by przemyslaw.daniel
def justify(text, width):
words = text.split()
by, to = divmod(width-sum(map(len, words)), max(len(words)-1, 1))
result = [word+' '*by+' '*(index < to) for index, word in enumerate(words)]
return ''.join(result).rstrip()
def text_formatting(text, width, style):
from textwrap import wrap
lines = wrap(text, width)
if style == 'j':
return '\n'.join([justify(line, width) for line in lines[:-1]]+lines[-1:])
align = {'l': '<', 'r': '>', 'c': '^'}[style]
return '\n'.join(f'{line:{align}{width}}'.rstrip() for line in lines)
April 25, 2019
Comments: