This quiz must be submitted in Gradescope by 12:00pm CST on Tuesday, Februrary 9, 2021.
This quiz focuses on the material from Worksheet 4.
Quizzes are INDIVIDUAL, closed book, and only allow access to specified resources. For this quiz you can access:
If you adapt code from any course documents (e.g. worksheet solutions), you need to indicate that in the required declaration comment at the top of the source file.
This quiz has 2 problems (numbered 2 and 3), each worth 4 points. Therefore, the final grading breakdown is:
Points | Item |
---|---|
3 | autograder |
4 | problem 2 |
4 | problem 3 |
11 | total |
The points assigned by the autograder based on syntax and docstring checking will be listed as problem 1 in Gradescope.
For the purposes of this problem, the digit sum of an integer is the sum of all of its decimal digits, e.g.
1205
is 1+2+5 = 8
99
is 9+9 = 18
Write a function called max_digit_sum
that takes any number of nonnegative integer arguments (but requires at least one argument), and determines which of its arguments has the largest digit sum, and returns that argument. If several arguments are tied for largest digit sum, the function should return the one appearing earliest in the argument list.
Put this function in a file called quiz4prob2.py
and submit it.
Examples:
max_digit_sum(1205,99) # 99 has largest digit sum 18
max_digit_sum(156213) # just returns the only argument given
max_digit_sum(11,101,53,120,1205)
# several arguments are tied for max digit sum of 8, so the one
# appearing earliest is returned, 53
In Python, a function that doesn't return a value implicitly returns the value None
.
Make a decorator called returns_something
that checks to make sure a function returns a value that is not equal to None
. If the function it decorates attempts to return None
, an exception should be raised.
Put this function in a file called quiz4prob3.py
and submit it.
Here is an example of defining two functions without the decorator:
def sum_plus_six(x,y):
"""Compute sum of x, y, and 6"""
return x+y+6
def calculate_but_no_return(x,y):
"""Do some arithmetic, but don't return anything"""
s = x+y
Here is the way the functions behave when defined without the decorator:
sum_plus_six(5,8)
calculate_but_no_return(11,94)
# No error will result, but no return value is obtained, either.
Now, here are the same functions defined with the decorator you have been asked to write applied to them.
@returns_something
def sum_plus_six(x,y):
"""Compute sum of x, y, and 6"""
return x+y+6
@returns_something
def calculate_but_no_return(x,y):
"""Do some arithmetic, but don't return anything"""
s = x+y
Here is the expected behavior of the functions with the decorator applied:
sum_plus_six(5,8)
calculate_but_no_return(11,94)
# RUNNING THIS SHOULD RAISE AN EXCEPTION
# because calculate_but_no_return tries to return None
# and the decorator won't allow that.
# We don't include the exception here, because the traceback
# shows the source code of the decorator, which is what you
# need to write!