def 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) == textWrappedAt40 print("Passed!")