RosettaCodeData/Task/Word-wrap/11l/word-wrap.11l
2023-07-01 13:44:08 -04:00

29 lines
1 KiB
Text
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

F word_wrap(text, line_width)
V words = text.split_py()
I words.empty
R
V wrapped = words[0]
V space_left = line_width - wrapped.len
L(word) words[1..]
I word.len + 1 > space_left
wrapped = "\n"word
space_left = line_width - word.len
E
wrapped = word
space_left -= 1 + word.len
R wrapped
V frog =
In olden times when wishing still helped one, there lived a king
whose daughters were all beautiful, but the youngest was so beautiful
that the sun itself, which has seen so much, was astonished whenever
it shone in her face. Close by the king's castle lay a great dark
forest, and under an old lime-tree in the forest was a well, and when
the day was very warm, the king's child went out into the forest and
sat down by the side of the cool fountain, and when she was bored she
took a golden ball, and threw it up on high and caught it, and this
ball was her favorite plaything.
L(width) (72, 80)
print(Wrapped at width":\n"word_wrap(frog, width))
print()