Something went wrong. Try again.
CMU Coding Bootcamp
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253def wrapText(text: str, lineSize: int) -> str: """Wrap text to a specified line size.""" lines = [] words = text.split() currentLine = "" for word in words: if len(currentLine) + len(word) > lineSize: currentLine = currentLine.rstrip() currentLine = currentLine.ljust(lineSize) currentLine = f"|{currentLine}|" lines.append(currentLine) currentLine = f"{word} " else: currentLine += f"{word} " else: currentLine = currentLine.rstrip() currentLine = currentLine.ljust(lineSize) currentLine = f"|{currentLine}|" lines.append(currentLine) return "\n".join(lines)
print("Testing wrapText()...", end="")text = """\This is some sample text. It is just sample text.Nothing more than sample text. Really, that's it."""
textWrappedAt20 = """\|This is some sample ||text. It is just ||sample text. Nothing||more than sample ||text. Really, that's||it. |"""
assert wrapText(text, 20) == textWrappedAt20
textWrappedAt30 = """\|This is some sample text. It ||is just sample text. Nothing ||more than sample text. Really,||that's it. |"""
assert wrapText(text, 30) == textWrappedAt30
textWrappedAt40 = """\|This is some sample text. It is just ||sample text. Nothing more than sample ||text. Really, that's it. |"""
assert wrapText(text, 40) == textWrappedAt40print("Passed!")