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)
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)
Get Answers For Free
Most questions answered within 1 hours.