Having trouble figuring this out Two words are anagrams if you can rearrange the
ID: 3920529 • Letter: H
Question
Having trouble figuring this out
Two words are anagrams if you can rearrange the letters of one to spell the second. For example, the following words are anagrams:
abets, baste, bates, beast, beats, betas
Pay attention to the letter count: hash and sash use the same three letters, but they are not anagrams.
1) At this point, I have a couple algorithms to tell if two words are anagrams:
def is_anagram(word1, word2):
''' function to decide if two words are anagrams'''
if len(word1) != len(word2):
return False
lst = [ch for ch in word2.lower() if ch in word1.lower()]
return len(word1) == len(lst)
print(is_anagram('Flap', 'Slap'))
print(is_anagram('Abut', 'Tuba'))
def are_anagrams(word1, word2):
word1 = word1.lower()
word2 = word2.lower()
if len(word1) != len(word2):
return False ###
else:
return word1[::-1] == word2
print(are_anagrams('flap', 'slap'))
print(are_anagrams('flap', 'PALF'))
print(are_anagrams('baste', 'tabes'))
I want to write a program that will mine the dictionary words.txt for sets of anagrams. I want it to take a command-line argument and be able to search a file. Throws an exception if the file doesn't exist. You might want to practice with the initial parts of words.txt. A 10,000 word list was used and found all but the last word in the list above.
Output: program should print the 10 largest sets of anagrams, such as the set above.
Hint: How can you tell quickly if two words are anagrams?
Hint: How can you avoid another sweep through the dictionary when you try to find anagrams of a new word?
Challenge: Write a program that finds all the anagrams in under 2 seconds. Time to beat is 0.4 seconds on an older laptop.
Hint: Dictionaries allow you to find a key quickly. What key would be most useful?
Hint: Try out your algorithms on a small set of words, such as this 5K list
There are three sets of anagrams of length 3 in this set:
(3, ['anergias', 'angaries', 'arginase'])
(3, ['amain', 'amnia', 'anima'])
(3, ['alien', 'aline', 'anile'])
Here is a file with the first 10K words
5K wordlist link: http://wikisend.com/download/997018/shortWords.txt
10K wordlist link: http://wikisend.com/download/358572/words10K.txt
Explanation / Answer
I have given you the correct algo to find 2 matching words as anagrams.. also shown what is wrong with your algorithm.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.