Skip to content

Instantly share code, notes, and snippets.

@drincruz
Created August 17, 2014 15:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drincruz/7d0eacd86ffc3d376f42 to your computer and use it in GitHub Desktop.
Save drincruz/7d0eacd86ffc3d376f42 to your computer and use it in GitHub Desktop.
Simple example to find out if two strings are anagrams of each other
#!/usr/bin/env python
"""
Example of how you can test if strings are anagrams
"""
def is_anagram(str1, str2):
"""
Check if str1 is an anagram of str2
Keyword arguments:
str1 - Input string 1
str2 - Input string 2
"""
# Strip whitespace
updated_str1 = "".join(str1.split(" "))
updated_str2 = "".join(str2.split(" "))
# Sort strings
updated_str1 = "".join(sorted(updated_str1))
updated_str2 = "".join(sorted(updated_str2))
# Test for equality
return updated_str1 == updated_str2
def main():
"""
Main
"""
print(is_anagram("a man a plan a canal panama", "abc"))
print(is_anagram("doggy", "y dogg"))
if '__main__' == __name__:
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment