Friday, October 15, 2010

Python - Anagram detector

# A python Anagram detector 
# manipulate anagrams with python 
# 
# Python could no doubt solve this in 
# many ways.... here is one: 
 
 
 
# determine whether a string is an anagram 
# of another string 
def isAnagramOf(attempt, original):
    # all the same case 
    attempt, original = attempt.lower(), original.lower()
    for character in attempt:
        position = original.find(character)
        if (position == -1):
            return False
        original = original.replace(character, '', 1)
    return True
 
 
original = "Texas Ranger" 
 
 
print("Is Rage an anagram of", original, "=",
      isAnagramOf("Anger", original))
print("Is Extra Angers an anagram of", original, "=",
      isAnagramOf("Extra Angers", original))
print("Is Extra Angered an anagram of", original, "=",
      isAnagramOf("Extra Angered", original))
 
#   my output: 
#   Is Rage an anagram of Texas Ranger = True 
#   Is Extra Angers an anagram of Texas Ranger = True 
#   Is Extra Angered an anagram of Texas Ranger = False 
 
 

No comments:

Post a Comment