Skip to content

Instantly share code, notes, and snippets.

@mortymacs
Last active May 26, 2017 08:22
Show Gist options
  • Select an option

  • Save mortymacs/e6b5664ad6fb6b3174dfb186f4ec6a5f to your computer and use it in GitHub Desktop.

Select an option

Save mortymacs/e6b5664ad6fb6b3174dfb186f4ec6a5f to your computer and use it in GitHub Desktop.
Simple Anagram in Python
def anagram(str1, str2):
str1 = str1.replace(" ", "").lower()
str2 = str2.replace(" ", "").lower()
if str1 == str2:
return True
if len(str1) != len(str2):
return False
for l in list(str1):
str2 = str2.replace(l, "")
str1 = str1.replace(l, "")
if str1.strip() == str2.strip():
return True
return False
anagram("public relations", "crap built on lies")
# True
anagram("Cook", "Book")
# False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment