Question

I have written an anagram solving algorithm, which does not work.

for word in wordlist: #Checking for equal length
    if sorted(word.replace("\n", "")) == sorted(anagram):
        possible.append(word)

I needed to use len(word) - 1 to take away the \n.

Was it helpful?

Solution

(1) I don't understand the "-1" in "len(word)-1" in your first loop.

(2) Your second loop has several problems:

It doesn't check to see whether the letters are the same, it checks to see whether each letter in the anagram is in the word. You're not using the count information, so you can't distinguish between bok and book. You're also removing from a sequence you're iterating over, which leads to unexpected behaviours.

For my part I'd simply use

sorted_anagram = sorted(anagram)
possibles = [word for word in wordlist if sorted(word) == sorted_anagram]

rather than explicit for loops.

Note that sorting the words is a kind of canonicalization process -- it makes sure that any two words which are anagrams of each other will be in the same format. Another approach to determine whether two things are anagrams would be to make sure that the letter counts are the same:

>>> from collections import Counter
>>> Counter('book')
Counter({'o': 2, 'k': 1, 'b': 1})
>>> Counter('obko')
Counter({'o': 2, 'k': 1, 'b': 1})
>>> Counter('bok')
Counter({'k': 1, 'b': 1, 'o': 1})
>>> 
>>> Counter('book') == Counter('boko')
True
>>> Counter('book') == Counter('bok')
False

OTHER TIPS

As mentioned in the comments the two evils that jump out at me are:

  1. Why do: if len(word) - 1 == len(anagram) ?
  2. Shortening a list while iterating it is a big nono. That line possible.remove(word) should be changed.

What about something like this:

anagramLength = len(anagram) # Get the value once to save CPU
possible1 = [word for word in wordlist if len(word)-1 == anagramLength] # List iteration
possible2 = [] # Make a new list that will be more constricted
for word in possible: #Checking for same letters
    for letter in anagram:
        if letter not in word:
            break
    else:
        possible2.append(word) # Only called if you don't break the above for loop

References to tools used:

  1. listIteration
  2. for..else
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top