Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Principles Programming And Performance

Authors: Patrick O'Neil, Elizabeth O'Neil

2nd Edition

1558605800, 978-1558605800

More Books

Students also viewed these Databases questions