Skip to content

Instantly share code, notes, and snippets.

@thekoc
Created November 2, 2016 09:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thekoc/6b5ee421a0727cd0ef4b3909de05f990 to your computer and use it in GitHub Desktop.
Save thekoc/6b5ee421a0727cd0ef4b3909de05f990 to your computer and use it in GitHub Desktop.
lines = """I am happy to join with you today in what will go down in history as the greatest demonstration for freedom in the history of our nation.
Five score years ago, a great American, in whose symbolic shadow we stand today, signed the Emancipation Proclamation. This momentous decree came as a great beacon light of hope to millions of Negro slaves who had been seared in the flames of withering injustice. It came as a joyous daybreak to end the long night of their captivity."""
def wrap_line(tokens):
if len(' '.join(tokens)) <= 80:
return ' '.join(tokens)
for i in range(len(tokens)):
if len(' '.join(tokens[:i+1])) > 80:
return ' '.join(tokens[:i]) + '\n' + wrap_line(tokens[i:])
wrapped = '\n'.join([wrap_line([t for t in line.split()]) for line in lines.splitlines()])
print(wrapped)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment