Reference no: EM131961516
Python 3
Do not import any package please
Q8: War and Peace
Your challenge for this problem is to create a function dictify that takes in 1 argument: book_name.
book_name: the string that is the name of the file to open
return: a dictionary containing the count of each length of word in sorted order. Eg. How many words total of length 1, 2, 3...?
Your output should look like (we cut out most of the dictionary): {6: 48342, 15: 254, 23: 2}
Hints:
Read in files using infile = open(filename), make sure that you are in the same directory.
Read each line by looping through with a simple for loop
Split the lines using line.split(" "), to get a list of the words
Since the book is long and complex, you might get zero length words (some tabs and spaces). Make sure to filter those out of your dictionary!
Your output dictionary should have its keys in sorted order, so {1: xxxx, 2: xxxxx, 3: xxxxx ...}
War and Peace. Part 2
Your challenge for this problem is to create a function process_book that takes in 2 arguments: book_name and mode.
book_name: the string that is the name of the file to open
mode: a string that is either "average" or "median"return: the "average" word length in the book or the "median" word length in the file, depending on the second argument.
Hint: We got a median word length of 4 for War and Peace.