Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(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

  1. 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

  2. Clean your data

    1. Make sure that all the words are in lower case

    2. Remove preceding and trailing spaces

    3. Remove the new line character

    4. Remove duplicates. Hint: convert your list to a set and back to a list

  3. 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.

  1. Ask the user to input a word

  2. Use the word_signature function to get the input words signature

  3. Iterate (loop) through the list of words in the English language

    1. If a signature of the input word matches signatures of any of the words in your list of words, you have your anagrams.

    2. Print the matched word

  4. In the end, your program should print the list of all anagrams for the provided word.

  5. 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

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

Big Data Systems A 360-degree Approach

Authors: Jawwad ShamsiMuhammad Khojaye

1st Edition

0429531575, 9780429531576

More Books

Students also viewed these Databases questions

Question

Are robots going to displace all workers? Explain your answer.

Answered: 1 week ago