.py
files containing your work. (If you upload a screenshot or other file format, you won't get credit.)This homework assignment must be submitted in Gradescope by 10am CST on Tuesday, September 28, 2021.
This homework assignment is about the topics of worksheet 5, i.e.
.split()
, .strip()
, .replace()
)Collaboration is prohibited, and you may only access resources (books, online, etc.) listed below.
The course materials you may refer to for this homework are:
This homework assignment has 2 problems, numbered 2 and 3. The grading breakdown is:
Points | Item |
---|---|
2 | Autograder |
4 | Problem 2 |
4 | Problem 3 |
10 | Total |
Ask your instructor or TA a question by email, in office hours, or on discord.
Gradescope will show the results of the automated syntax check of all submitted files as the score for problem 1.
Write a function called string_stats(s)
that accepts a string s
and returns a dictionary with the following keys and values:
"length"
-> the length of s
"words"
-> the number of words in s
, as determined by the .split()
method"exclamation_count"
-> the number of times the character "!"
appears in s
The function must not print anything to the terminal. Do not use the print()
function at all.
You don't need to write any code to actually call this function, but that might be helpful while you're preparing it (for testing). Here are some sample inputs and expected return values from the function. What you see below displays the return value after each function call, just as the Python REPL would do. The function itself isn't printing anything.
string_stats("Did you see the new project description? It's available now!")
string_stats("wheeee!!!!!!!!!!!!!")
string_stats("nothing to see here")
Put the function definition in a file homework5.py
. The next problem will have you add another function to that file.
Write a function random_letter()
that returns a randomly selected capital letter from the alphabet (A-Z).
The function must not print anything to the terminal. Do not use the print()
function at all.
You don't need to write any code to actually call this function, but that might be helpful while you're preparing it (for testing). Here are some sample calls and the return values from such a function (but of course, due to the random element, it will behave differently every time it is run):
random_letter()
random_letter()
random_letter()
Put the definition in a file homework5.py
(i.e. the same file where you put the function requested in problem 2). Make sure the file imports any modules your function requires.