Skip to content

Instantly share code, notes, and snippets.

@Abuton
Created February 6, 2022 22:52
Show Gist options
  • Save Abuton/41cf829b6e0595fb94ace415bebeb57f to your computer and use it in GitHub Desktop.
Save Abuton/41cf829b6e0595fb94ace415bebeb57f to your computer and use it in GitHub Desktop.
def replace_word(roots: list, sentence: str) -> str:
output = []
sentence_list = sentence.split()
for word in sentence_list:
output.append(word)
for r in roots:
if word.startswith(r):
output[-1] = r
return " ".join(output)
replace_word(roots=['cat', 'bat', 'rat'], sentence='the cattle was rattled by battery')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment