Return to Snippet

Revision: 51399
at September 23, 2011 06:12 by eazyigz


Initial Code
one =  raw_input('Enter first word/phrase: ')
two = raw_input('Enter second word/phrase: ')

# Check if one phrase has exactly the same letter set as the other.
def isAnagram(one, two):
  # First divide each phrase into a list of words, then join the words into
  # a string.  Then compare the alphabetically-sorted strings for equality.
  # Apostrophes are stripped from the words.
  list1 = one.split()
  list2 = two.split()
  word1 = ""; word2 = ""
  sorted_joined_str1 = ""; sorted_joined_str2 = ""
  for word in list1:
      word1 += word
  sorted_joined_str1 += ''.join(sorted(word1, key=str.lower)).lower().replace("'","")

  for word in list2:
      word2 += word
  sorted_joined_str2 += ''.join(sorted(word2, key=str.lower)).lower().replace("'","")

  if sorted_joined_str1 == sorted_joined_str2: return True
  else: return False


if isAnagram(one, two):
        print one + " and " + two + " are anagrams of each other"
else: print one + " and " + two + " are NOT anagrams of each other"

Initial URL


Initial Description


Initial Title
Anagram checker in Python

Initial Tags
python

Initial Language
Python