Skip to content

Instantly share code, notes, and snippets.

Last active October 3, 2022 21:39
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
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