Bad solution (the trap):
look at all the possible permutations of wordA and find them in listA.
This is a terrible solution and has a complexity of countOfCharactersInWord(wordA)!
where ! is the factorial symbol :)
Good solution:
take all the characters in wordA and sort them. Then compare it with the each item in listA first sorting the characters of that item as well :)
In python :
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
#the wordlist file is the SCOWL wordlist downloaded from : http://wordlist.sourceforge.net/ | |
file = open("./SCOWL/wordlist.txt") | |
words =[line[:-1] for line in file.readlines()] #remove the newline character. | |
file.close() | |
#basic anagram comparion algorithm | |
def AreSameCharacters(word1,word2): | |
word1 = list(word1) | |
word2 = list(word2) | |
word1.sort() | |
word2.sort() | |
word1 = "".join(word1).lower() | |
word2 = "".join(word2).lower() | |
return word1==word2 | |
def IsAnagramEqual(word1,word2): | |
if (word1==word2): | |
return False #we do not list the same word as an anagram :) | |
if len(word1) != len(word2): | |
return False | |
if not AreSameCharacters(word1,word2): | |
return False | |
return True | |
#word input by user | |
word1 = str(raw_input("Please Input Your Word: ")).strip().lower() | |
#single word comparisons: | |
for word2 in words: | |
if IsAnagramEqual(word1,word2): | |
print word2 |
This yields for my friend "omair" the following interesting (i made them bold) anagrams:
mario arimo moria omari maori moira
I used the SCOWL wordlist for this. You can get the complete working code (including the wordlist) from here:
https://bitbucket.org/basarat/pyanagram/overview
Enjoy!
No comments:
Post a Comment