Question
PYTHON3; DO NOT IMPORT ANY PACKAGES Please do all of number 1 1. a) Create a function that reads in a file and returns a
PYTHON3; DO NOT IMPORT ANY PACKAGES
Please do all of number 1
1.
a)
Create a function that reads in a file and returns a single dictionary with the counts of all characters (including letters, numbers, spaces or any other characters) in this file.
Note that the new line character at the end of each line should also count. In the doctest, escape it once more (write as \ ).
For Windows users: If your output dictionary contains an extra " " key, you can manually delete this key before returning the dictionary. It is an operating system implementation difference that you should not worry about.
def count_letters(inpath):
"""
>>> count_letters("infiles/blank.txt")
{}
>>> count_letters("infiles/input1.txt")
{'H': 1, 'e': 1, 'l': 2, 'o': 1, ' ': 1, '.': 1, '\ ': 1, 'a': 5}
>>> count_letters("infiles/input3.txt")
{'a': 3}
"""
# YOUR CODE GOES HERE #
b)
Create a function that reads multiple files and returns a single dictionary with the counts of all characters in all files combined. You should reuse the count_letters() function you just implemented.
Note: Be careful if you are trying to use the dict.update() function, as it may not have the functionality that you intend. You can read the hyperlinked article above to see what .update() does.
def count_letters_multiple_files(*inpaths):
"""
>>> count_letters_multiple_files("infiles/input1.txt",
... "infiles/blank.txt")
{'H': 1, 'e': 1, 'l': 2, 'o': 1, ' ': 1, '.': 1, '\ ': 1, 'a': 5}
>>> count_letters_multiple_files("infiles/input2.txt")
{'z': 7, '\ ': 6}
>>> count_letters_multiple_files('infiles/blank.txt',
... 'infiles/input2.txt', 'infiles/input3.txt')
{'z': 7, '\ ': 6, 'a': 3}
"""
# YOUR CODE GOES HERE #
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started