Skip to content

Instantly share code, notes, and snippets.

@amingilani
Created June 26, 2021 12:44
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 amingilani/0536278e306db3eff0072b381d17dd4e to your computer and use it in GitHub Desktop.
Save amingilani/0536278e306db3eff0072b381d17dd4e to your computer and use it in GitHub Desktop.
Function that takes in a string of words and returns the subset of palindromes
def isolate_palindromes(words: str) -> str:
"""Takes in a string of words and returns the subset of palindromes
Args:
words (str): string of words, e.g. "bar kaak"
Returns:
str: any palindromes within the string of words passed, e.g. "kaak"
"""
return [word for word in words.split() if word == word[::-1]]
# Replace the value of this contant with the one you want to use
WORDS = "ballot kayak rat racecar hellcat"
print(isolate_palindromes(WORDS))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment