.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.
def string_stats(s):
'''
Returns a dictionary with statistics about a string `s`
'''
# Use curly braces to define a dictionary
stats = { "length": len(s),
"words": len(s.split()),
"exclamation_count": len( [c for c in s if c == "!"]) }
return stats # Return the dictionary we made
# test code
print(string_stats("Did you see the new project description? It's available now!"))
print(string_stats("wheeee!!!!!!!!!!!!!"))
print(string_stats("nothing to see here"))
Remark: There is also a method of str
in Python that will count how many times a character appears. Using this, the number of exclamation points could be counted using
s.count("!")
in place of the list comprehension appearing in the solution above.
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.
import random
def random_letter():
'''
Return a capital letter chosen from the alphabet at random
'''
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
letter = random.choice(alphabet)
return letter
# test code
for i in range(3):
print(random_letter())