Question
(Python) I can't get the code to output. Help plz. This is the prompt: Tasks Read data from the words.txt text file and store it
(Python) I can't get the code to output. Help plz. This is the prompt:
Tasks
-
Read data from the words.txt text file and store it as a list of individual words. Hint: split the contents of the text file on the new line character
-
Clean your data
-
Make sure that all the words are in lower case
-
Remove preceding and trailing spaces
-
Remove the new line character
-
Remove duplicates. Hint: convert your list to a set and back to a list
-
-
Integrate the following function in your code.
def word_signature(word): sorted_word = sorted(word) word_letters = ''.join(sorted_word) return word_letters |
This function determines a words signature. Python basically treats strings as lists of characters, so a words signature is just a string that contains all of the words letters sorted in alphabetical order. For example, the words lives and elvis have the same signature: eilsv.
-
Ask the user to input a word
-
Use the word_signature function to get the input words signature
-
Iterate (loop) through the list of words in the English language
-
If a signature of the input word matches signatures of any of the words in your list of words, you have your anagrams.
-
Print the matched word
-
-
In the end, your program should print the list of all anagrams for the provided word.
-
For example, if you search for the word python, your program should find 'typhon' and 'phyton' as the anagrams
(Below is the my code)
_____________________________________________________________
file = open('words.txt').read()
file.split(' ')
text = file.lower()
all_text = text.strip()
wordset = set(all_text)
word = list(wordset)
def word_signature(word):
sorted_word = sorted(word)
word_letters = ''.join(sorted_word)
return word_letters
user = input("Enter word: ")
input_signature = word_letters(user)
for word in word:
if word_signature(word) == input_signature:
print(str(word))
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started