Question

In Python 3, Write a program that reads in a text file that consists of some...

In Python 3, Write a program that reads in a text file that consists of some standard English text. Your program should count the number of occurrences of each letter of the alphabet, and display each letter with its count, in the order of increasing count. What are the six most frequently used letters?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any queries write a comment in understood upvote Thak you

SOLUTION:

#open file
f = open("testingDoc.txt", "r")
#read data from file
filedata=f.read()
#dictionary to store frequency
freq = {}
#parse through each alphabet of file data
for i in filedata:
#here we are considering only alphabets we are checking ascii values of a-z and A-Z
if((ord(i)>=65 and ord(i)<=90) or(ord(i)>=97 and ord(i)<=127)):
#here i am storing to freq dictionary by converting to lower case now "a" and "A" are same
i=i.lower()
#check whether that data is present in diction if exist increase count else add to didctionary
if i in freq:
freq[i] += 1
else:
freq[i] = 1
#sort dictionary
sortedData=sorted(freq.items(), key = lambda kv:(kv[1], kv[0]))
print(sortedData)
#print last six most used
print("Six most used alphabets")
for i in range(len(sortedData)-1,len(sortedData)-7,-1):
print(sortedData[i])

CODE IMAGE AND OUTPUT:

- 0 x lè Python 3.7.2 Shell File Edit Shell Debug Options Window h, 17), (, 24)] Help Lè occurencesofalphabetfile.txt - C:

Add a comment
Know the answer?
Add Answer to:
In Python 3, Write a program that reads in a text file that consists of some...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. Write a python program that reads a file and prints the letters in increasing order...

    1. Write a python program that reads a file and prints the letters in increasing order of frequency. Your program should convert entire input to lower case and only counts letters a-z. Special characters or spaces should not be counted. Each letter and it's occurrences should be listed on a separate line. 2. Test and verify your program and it's output. ---Please provide a code and a screenshot of the code and output. Thank you. ----

  • JAVA Code: Complete the program that reads from a text file and counts the occurrence of...

    JAVA Code: Complete the program that reads from a text file and counts the occurrence of each letter of the English alphabet. The given code already opens a specified text file and reads in the text one line at a time to a temporary String. Your task is to go through that String and count the occurrence of the letters and then print out the final tally of each letter (i.e., how many 'a's?, how many 'b's?, etc.) You can...

  • (Python 3) Write a program that reads the contents of a text file. The program should...

    (Python 3) Write a program that reads the contents of a text file. The program should then create a dictionary in which the keys are individual words found in the file and the values are the number of times each word appears and a list that contains the line numbers in the file where the word (the key) is found. Then the program will create another text file. The file should contain an alphabetical listing of the words that are...

  • (Count the occurrences of each keyword) Write a python program that reads in a Python source...

    (Count the occurrences of each keyword) Write a python program that reads in a Python source code file and counts the occurrence of each keyword in the file. Your program should prompt the user to enter the Python source code filename.

  • Letter Count : Write a Python script that reads a file and outputs the number of...

    Letter Count : Write a Python script that reads a file and outputs the number of words that start with each letter. This means that for every letter we want to count the total number of (nonunique) words that begin with that letter. In your implementation you should ignore the letter case, i.e., consider all words as lower case. Output the results in the following format (below values are hypothetical): a or A: 120 b or B: 460 c or...

  • Question 2 Write a program that will read in a line of text up to 100...

    Question 2 Write a program that will read in a line of text up to 100 characters as string, and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by whitespace, a period, a comma, or the beginning or end of the line. You can assume that the input consists entirely of letters, whitespace, commas, an<d periods. When...

  • C++ please Write a program that reads the following sentences from a file: I am Sam...

    C++ please Write a program that reads the following sentences from a file: I am Sam Sam I am That Sam I am That Sam I am I do not like that Sam I am Do you like green eggs and ham I do not like them But I do like spam! You will first have to create the text file containing the input, separate from your program. Your program should produce two output files: (i) (ii) one with the...

  • in c++ please. Write a program that reads the contents of a text file. The program...

    in c++ please. Write a program that reads the contents of a text file. The program should create a map in which the keys are the individual words found in the file and the values are the number of times each word appears. For example, if the word "the" appears 128 times, the map would contain an element with "the" as the key and 128 as the value. The program should either display the frequency of each word or create...

  • File Encryption and Decryption chapter 9 programming exercise #3 Design and write a python program to...

    File Encryption and Decryption chapter 9 programming exercise #3 Design and write a python program to successfully complete chapter 9 programming exercise #3. File Encryption and Decryption Write a program that uses a dictionary to assign “codes” to each letter of the alphabet. For example: codes = { ‘A’ : ‘%’, ‘a’ : ‘9’, ‘B’ : ‘@’, ‘b’ : ‘#’, etc . . .} Using this example, the letter A would be assigned the symbol %, the letter a would...

  • Write a Python program that reads text from a file, encrypts it with a Caesar Cipher,...

    Write a Python program that reads text from a file, encrypts it with a Caesar Cipher, and displays the encrypted text. Do not process punctuation. Convert the original string to all lower-case before encrypting it.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT