0

So my code gets two words, and checks if one is the anagram of another one.

However doesn't work if multiple letters are exchanged, although I tried to account for that.

storedword = input("Enter your primary word \t")
global word 
word = list(storedword)


word3 = input("Enter anagram word \t")
word3lowercase = word3.lower()
anaw = list(word3lowercase)


counter = int(0)
letterchecker = int(0)
listlength = len(word)
newcounter = int(0)
if len(anaw) != len(word):
    print ("not anagram")

if len(anaw) == len(word):
    while counter < listlength and newcounter < listlength:
        tempcount = 0
        if anaw[counter] == word[newcounter]:
            temp = word[newcounter]
            word[newcounter] = word[tempcount]
            word[tempcount]=temp
            letterchecker +=1
            counter +=1
            tempcount +=1
            newcounter = int(0)

        else:
            newcounter +=1

if counter == len(word):
    print ("anagram")
else:
    print ("not anagram")

I think it's gone somewhere wrong after the if len(anaw) section, for example if the primary word is "hannah", and the secondary word is "hannnn", it thinks it's an anagram.

Floern
  • 33,559
  • 24
  • 104
  • 119
I Haque
  • 29
  • 5

4 Answers4

3

There is much simpler logic that can be implemented here, even without using sorted and such. Let's assume you have a function anagram:

def anagram(word1, word2):
    if len(word1) != len(word2):
        return False

    def char_count(word):
        char_count = {}
        for c in word:
            char_count[c] = char_count.get(c, 0) + 1
        return char_count

    cr1 = char_count(word1)
    cr2 = char_count(word2)
    return cr1 == cr2

You can test this with:

>>> print(anagram("anagram", "aanragm"))
True
>>> print(anagram("anagram", "aangtfragm"))
False

And for future readers, a super simple pythonic solution might be using Counter:

from collections import Counter
>>> Counter(word1) == Counter(word2)

Or using sorted:

>>> sorted(word1) == sorted(word2)
Idos
  • 15,053
  • 14
  • 60
  • 75
  • You can use a `set` to avoid re-checking similar characters. Also, I'd add the `Counter(word) == Counter(otherword)` solution for the sake of future readers. – Reut Sharabani Mar 01 '16 at 15:29
  • 1
    @ReutSharabani I figured he might not be able to import/use `Counter`. Regarding the `set` I might edit later (not next to a computer so I don't want to make any changes without testing them). You are welcome to edit my answer tho ;) – Idos Mar 01 '16 at 15:31
  • I know he isn't, but the answer doesn't have to be too specific. If a future reader runs into this he may want the nicer solution. That's why I wrote "for the sake of *future* readers" :-) – Reut Sharabani Mar 01 '16 at 15:32
  • if it is for future readers, you should precise `from collections import Counter` – mvelay Mar 01 '16 at 15:33
  • 1
    Haha okay sure I'll edit this a lil later then for all the millions of future readers who will flock to this answer like wild geese ;p – Idos Mar 01 '16 at 15:33
  • For the other answers that aren't restricted. [Determine if 2 lists have the same elements, regardless of order?](http://stackoverflow.com/q/8866652/5012922) – SirParselot Mar 01 '16 at 15:49
0
newcounter = int(0)

This is the line that causes the trouble (in the while loop). Because of it you start checking the word from the beginning again. I think you want it to be newcounter=letterchecker. Since already used characters are put to the front of word they are ignored if you start with letterchecker

Tell me if it works

Edit:Checked with example given, seems to work.

Jan
  • 1,504
  • 10
  • 15
0

Without using sort you could use the following approach. It removes a letter from an array of a characters of the second word. The words are only anagrams if there are no letters left (and the words are the same length to start with and have a length larger than zero):

word1="hannah"
word2="nahpan"

chars1= list(word1)
chars2= list(word2)

if len(chars1)==len(chars2) and len(chars1)>0:
    for char in chars1:
        if char not in chars2:
           break
        chars2.remove(char)                   


if len(chars2)==0:
    print "Both words are anagrams"
else:
    print "Words are not anagrams"
Alex
  • 21,273
  • 10
  • 61
  • 73
-2

[EDIT THIS IS JUST FOR PALINDROMES I CANT READ]

Here is something a bit more simple:

storedword = input("Enter your primary word \t")
word3 = input("Enter anagram word \t")
if storedword == word3[::-1]:
    print "Is Anagram"
else:
    print "Is not anagram"