Question

the text file is given but extremely long do i would appreciate the just the code...

the text file is given but extremely long do i would appreciate the just the code in itself and if u could explain how to open a txt file
thank you
word_anagrams(word, wordbook):
'''(str, list of str) -> list of str
- a string (representing a word)
- wordbook is a list of words (with no words duplicated)

This function should call test_letters function.

The function returs a (lexicographicaly sorted) list of anagrams of the given word in wordbook
>>> word_anagrams("listen", wordbook)
['enlist', 'silent', 'tinsel']
>>> word_anagrams("race", wordbook)
['acre', 'care']
>>> word_anagrams("care", wordbook)
['acre', 'race']
>>> word_anagrams("year", wordbook)
[]
>>> word_anagrams("ear", wordbook)
['are', 'era']
'''

Homework Answers

Answer #1

def test_letters(s1,s2):

s1 = sorted(s1.lower())

s2 = sorted(s2.lower())

return s1 == s2

def word_anagrams(string,wordbook):

anagrams_list = []

for word in wordbook:

if test_letters(string, word):

anagrams_list.append(word)

return anagrams_list

str = input("Enter string : ")

wordbook = []

with open("abc.txt", "r") as data:

for line in data:

word = line.strip()

wordbook.append(word)

print(word_anagrams(str,wordbook))

Output screenshot:

NOTE: YOU MUST MENTION YOUR FILENAME IN PLACE OF abc.txt .

If any problems occur contact me. Thanks

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT