Created
August 17, 2014 15:53
-
-
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
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
#!/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