Question

WRITE USING PYTHON PROGRAMMING THE CODE GIVEN BELOW HAS SOME ERRORS WHICH NEED TO BE SOLVED....

WRITE USING PYTHON PROGRAMMING

THE CODE GIVEN BELOW HAS SOME ERRORS WHICH NEED TO BE SOLVED. ALSO THE 2 POINTS MENTIONED BELOW SHOULD BE PRESENT IN THE CODE

Write a script that calculates the 3 longest words of a text stored in a file and print them from the longest to the smaller of the 3.

Please note:

1. The name of the file is word_list.csv and it doesn’t need to be asked to the user (meaning the name will be in the code)

2. Assume that the file contains n records, each one composed by 1 word. Words can be present more than once, but only unique words need to be considered

A sample word_list.csv file is attached for testing.

THIS CODE NEEDS TO BE MOSIFIED SO AS THE ABOVE 2 CONDITIONS ARE SATISFIED (TOP 3 LONGEST UNIQUE WORDS SHOULD BE MENTIONED IN THE OUTPUT)

handle = open('word_list.csv','r')
top3 = ["","",""]
for line in handle:    #For each line in the file, strip the input and put it into the word variable
    word = line.strip()    #Compare the length of each incoming word to the length of each word in each position
    for i in range(0,3):
        top3.sort(key = len)
        if (len(word) > len(top3[i])):
            top3[-i] = word#Print the words
print ("\nThe 3 longest words are:", top3)

THIS CODE NEEDS TO BE MOSIFIED SO AS THE ABOVE 2 CONDITIONS ARE SATISFIED (TOP 3 LONGEST UNIQUE WORDS SHOULD BE MENTIONED IN THE OUTPUT)

Homework Answers

Answer #1
handle = []
with open('word_list.csv','r') as file: 

        # reading each line      
        for line in file: 

                # reading each word              
                for word in line.split(): 

                        handle.append(word)
   

handle = list(set(handle))  #remove duplicates

top3 = ["","",""]
for line in handle:    #For each line in the file, strip the input and put it into the word variable
    word = line.strip()    #Compare the length of each incoming word to the length of each word in each position
    for i in range(0,3):
        top3.sort(key = len)
        if (len(word) > len(top3[i])):
            top3[-i] = word#Print the words
print ("\nThe 3 longest words are:", top3)
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
I need python code for this. Write a program that inputs a text file. The program...
I need python code for this. Write a program that inputs a text file. The program should print the unique words in the file in alphabetical order. Uppercase words should take precedence over lowercase words. For example, 'Z' comes before 'a'. The input file can contain one or more sentences, or be a multiline list of words. An example input file is shown below: example.txt the quick brown fox jumps over the lazy dog An example of the program's output...
Write a python code to print (any given file) file as a csv format by using...
Write a python code to print (any given file) file as a csv format by using MRJob only. code must be able to run stand-alone MRJob application. For submission to your work: Your final hand should be a single file name BDM.py that takes exactly one arguments for the input path. output will be handled through redirection. Sample run: python BDM.py sample.csv > output.csv
So, i have this code in python that i'm running. The input file is named input2.txt...
So, i have this code in python that i'm running. The input file is named input2.txt and looks like 1.8 4.5 1.1 2.1 9.8 7.6 11.32 3.2 0.5 6.5 The output2.txt is what i'm trying to achieve but when the code runs is comes up blank The output doc is created and the code doesn't error out. it should look like this Sample Program Output 70 - 510, [semester] [year] NAME: [put your name here] PROGRAMMING ASSIGN MENT #2 Enter...
Write a python program that will perform text analysis on an input text using all of...
Write a python program that will perform text analysis on an input text using all of the following steps: 1. Take the name of an input file as a command line argument. For example, your program might be called using python3 freq.py example1 to direct you to process a plain text file called example. More information is given on how to do this below. 2. Read the contents of the file into your program and divide the text into a...
For this lab assignment you will need to write some code and create some graphs. You...
For this lab assignment you will need to write some code and create some graphs. You may use excel to create your graphs, or other language of your choice. Your data needs to demonstrate the experimental running time for Selection Sort (code in book), Merge Sort (code in book), and the Arrays.sort() method. Here is a basic outline of how you need to code this assignment. 1) Create several arrays of size 100, 1000, 10000, 100000, 1000000, … (you need...
write a code in python Write the following functions below based on their comments. Note Pass...
write a code in python Write the following functions below based on their comments. Note Pass is a key word you can use to have a function the does not do anything. You are only allowed to use what was discussed in the lectures, labs and assignments, and there is no need to import any libraries. #!/usr/bin/python3 #(1 Mark) This function will take in a string of digits and check to see if all the digits in the string are...
Assignment Statement Use the skeleton file starter code (below) to create the following classes using inheritance:...
Assignment Statement Use the skeleton file starter code (below) to create the following classes using inheritance: ⦁   A base class called Pet ⦁   A mix-in class called Jumper ⦁   A Dog class and a Cat class that each inherit from Pet and jumper ⦁   Two classes that inherit from Dog: BigDog and SmallDog ⦁   One classes that inherit from Cat: HouseCat The general skeleton of the Pet, Dog, and BigDog classes will be given to you (see below, "Skeleton", but...
Using python 3.5 or later, write the following program. A kidnapper kidnaps Baron Barton and writes...
Using python 3.5 or later, write the following program. A kidnapper kidnaps Baron Barton and writes a ransom note. It is not wrriten by hand to avoid having his hand writing being recognized, so the kid napper uses a magazine to create a ransom note. We need to find out, given the ransom note string and magazine string, is it possible to given ransom note. The kidnapper can use individual characters of words. Here is how your program should work...
Write a code in c++ using linear insertion following the steps below. Comment your work. 1....
Write a code in c++ using linear insertion following the steps below. Comment your work. 1.    Ask the user for the name of a file containing data. If it does not exist, the program should display an error, then ask for a new file name. Entering an asterisk (*) as the first and only character on a line should terminate the program. 2.     You can use a statically-allocated one-dimensional array of doubles for this with length 100. You...
Please do it in Python Write the simplest program that will demonstrate iteration vs recursion using...
Please do it in Python Write the simplest program that will demonstrate iteration vs recursion using the following guidelines - Write two primary helper functions - one iterative (IsArrayPrimeIter) and one recursive (IsArrayPrimeRecur) - each of which Take the array and its size as input params and return a bool. Print out a message "Entering <function_name>" as the first statement of each function. Perform the code to test whether every element of the array is a Prime number. Print out...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT