Skip to content

Instantly share code, notes, and snippets.

@dogweather
Last active October 3, 2022 21:39
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 dogweather/51d51256936c032d3eea1afb8be3bf91 to your computer and use it in GitHub Desktop.
Save dogweather/51d51256936c032d3eea1afb8be3bf91 to your computer and use it in GitHub Desktop.
Python implementation of five string operations
from typing import Generator
def clean_up(s: str) -> str:
return next(split_into_sentences(fix_hyphenation(fix_whitespace(s))))
def fix_whitespace(s: str) -> str:
return s.replace("\n", " ")
def fix_hyphenation(s: str) -> str:
return s.replace("- ", "")
def split_into_sentences(s: str) -> Generator[str, None, None]:
return (ensure_ends_with_period(sentence) for sentence in s.split(". "))
def ensure_ends_with_period(sentence: str) -> str:
return sentence + ("" if sentence.endswith(".") else ".")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment