Question: Assignment In this lab, you are going read a text file containing letters from various text documents.Your goal is to count the number of occurrences


Assignment In this lab, you are going read a text file containing letters from various text documents.Your goal is to count the number of occurrences of each different letter The files you read will all contain only uppercase letters A-Z each letter is separated by a single white-spaces (we do not want to count the whitespaces). The file may or may not contain newlines (we do not want to count the newlines) Functions For this lab you will write the following 3 functions readLettersFromFile This function will take a string filename as an argument and return the characters from the file in a list. Hint: what is the difference between string.split0 and string.split Hint: what is the difference between file.read0 and file.readlines0 and file.readline countLetters This function will take a list of characters and return a dictionary where the keys are the letters and the value is the number of times that character appeared in the list. sortByCount This function will take a dictionary and sort by the count of each letter, then the letter (if the counts are the same). This function should return a list of tuples in ascending sorted order Hint: Loop over the key-value pairs of the dictionary Create a new list that holds a tuple of the value then the key. Example:(value, key) Then you should sort and return the resultant list. Example File: sample.txt A P PIE 0/10 LAB ACTIVITY 11.5.1: L11 - Lab Additional files provided by your instructor Download letters1. letters3let , letters2 ,and sample.txt main.py Load default template... 1 def readLettersFromFile(filename) """Return all characters from file in a list""" 3 return [] 4 5 def countLetters(lettersFromFile): key : value pairs (letter,# times letter appears in file)""" of """Return return f dictionary 9 def sortByCount (count) 10 11 return [ 12 13 def main): 14 15 16 17 18 19 count -countLetters (letters) 26 21 22 countSorted=sortByCount (count) ""Return list of tuples (value,key), sorted by value""" ""DO NOT CHANGE THIS""" filenameinput("filename? ") letters readLettersFromFile(filename) print("Letters:", letters) print("Counted Letters", count) print("Sorted:", countSorted) 24 25 if _name "main__" 26 main()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
