Last active
October 3, 2022 21:39
-
-
Save dogweather/51d51256936c032d3eea1afb8be3bf91 to your computer and use it in GitHub Desktop.
Python implementation of five string operations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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