Question

Exercise 3: Write a program that reads a file and prints the letters in decreasing order of frequency. Your program should convert all the input to lower case and only count the letters a-z. Your program should not count spaces, digits, punctuation, or anything other than the letters a-z. Find text samples from several different languages and see how letter frequency varies between languages. Compare your results with the tables at https://wikipedia.org/wiki/Letter_frequencies.

PYTHON PLEASE the text can be found here :  www.py4e.com/code3/mbox-short.txt on here!! thank you so much

Enter file name: mbox-short.txt 0.09290243193820177 e 0.08926221523422145 a 0.07680344538820433 i 0.0713345752226001 O 0.0694

it should be in percentage if that is possible!

THANK YOU

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

Given below is the code for the question. PLEASE MAKE SURE INDENTATION IS EXACTLY AS SHOWN IN IMAGE.
Please do rate the answer if it helped. Thank you.

filename = input('Enter file name: ')
f = open(filename, 'r')
frequencies = {}
totalLetters = 0
for line in f.readlines():
   line = line.lower()
   for c in line:
       if c.isalpha():
           totalLetters += 1
           if c in frequencies: #letter is already in dictionary
               frequencies[c] += 1
           else: #letter not yet in dictionary
               frequencies[c] = 1
f.close()
#now make a list of tuples with (freq, char) and then sort on decreasing frequency
freqlist = []
for c in frequencies:
   freqlist.append((frequencies[c], c))

freqlist.sort(reverse = True)
#now print the sorted results

for t in freqlist:
   occurs = t[0]
   letter = t[1]
   percent = occurs / totalLetters
   print('{} {} \t {}%'.format(letter, percent, round(percent * 100, 2)))

Enter file name: short.txt e 0.09290243193820177 9.29% a 0.08926221523422145 8.93% i 0.07680344538820433 7.68% 0 0.0713345752

1 2 3 4 5 로 6 7 00 > 8 9 filename = input(Enter.file name: ) f = open(filename, r) frequencies = { } totalLetters = 0 for

Add a comment
Know the answer?
Add Answer to:
Exercise 3: Write a program that reads a file and prints the letters in decreasing order...
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. ----

  • 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?

  • Following should be done in C++: Create a program that reads a text file and prints...

    Following should be done in C++: Create a program that reads a text file and prints out the contents of the file but with all letters converted to uppercase. The name of the file to be read by the program will be provided by the user. Here are some example session: Contents of "data.txt": Hello, World! User input: data.txt Program output: HELLO, WORLD! Contents of "data.txt": tHiS FiLe HaS mIxEd CaSeS User input: data.txt Program output: THIS FILE HAS MIXED...

  • Write a program that Reads a string from cin Prints the first and last letter without...

    Write a program that Reads a string from cin Prints the first and last letter without space in between, followed by endl For example, For an input seattle your program prints se(endl). For an input newyork your program prints nk (endl). You can assume that the length of the string is always greater than 1. LAB 0/10 ACTIVITY 1.9.1: Week2-4 Zip main.cpp Load default template. #include <iostream> using namespace std; 4int mainO 6 /* 8return Type your code here. /...

  • Write a program that reads a string from the keyboard and computes the two arrays of...

    Write a program that reads a string from the keyboard and computes the two arrays of integers upperCase, and lowerCase, each of size 26. The first one represents the frequency of upper case letters and the second one represents the frequency of lower case letters in the input string. After computing these arrays, the program prints the frequencies in the following format: The frequency of the letter A= The frequency of the letter B= Assume the input string contains only...

  • Write a complete C program that inputs a paragraph of text and prints out each unique...

    Write a complete C program that inputs a paragraph of text and prints out each unique letter found in the text along with the number of times it occurred. A sample run of the program is given below. You should input your text from a data file specified on the command line. Your output should be formatted and presented exactly like the sample run (i.e. alphabetized with the exact spacings and output labels). The name of your data file along...

  • Write a program that reads in a text file, infile.txt, and prints out all the lines...

    Write a program that reads in a text file, infile.txt, and prints out all the lines in the file to the screen until it encounters a line with fewer than 4 characters. Once it finds a short line (one with fewer than 4 characters), the program stops. For your testing you should create a file named infile.txt. Only upload your Python program, I will create my own infile.txt. Please use a while-loop BUT do not use break, Exit or Quit...

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

  • Write a C++ program that inputs a single letter and prints out the corresponding digit on the...

    Write a C++ program that inputs a single letter and prints out the corresponding digit on the telephone.The letters and digits on a telephone are grouped this way:2 = ABC 4 = GHI 6 = MNO 8 = TUV3 = DEF 5 = JKL 7 = PRS 9 = WXYNo digits correspond to either Q or Z. For these two letters, your program should print a messageindicating that they are not used on a telephone. If a letter in lower...

  • please use c++ please write down the input file information please add comments for some codes...

    please use c++ please write down the input file information please add comments for some codes please do not use #include <bits/stdc++.h> we did not learn it yet thank you CSIT 575-Take Home Lab #11: Arrays To learn to code, compile and run a program processing characters. Assignment Plan and code a top-down modular program utilizing ARRAYS to solve the following problem, using at least 3 functions (called from the main() section or from another function) to solve the problem....

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