A document from MCS 275 Spring 2021, instructor David Dumas. You can also get the notebook file.

Quiz 4

MCS 275 Spring 2021 - David Dumas

Instructions:

Deadline

This quiz must be submitted in Gradescope by 12:00pm CST on Tuesday, Februrary 9, 2021.

Topic

This quiz focuses on the material from Worksheet 4.

Resources you are allowed to consult

Quizzes are INDIVIDUAL, closed book, and only allow access to specified resources. For this quiz you can access:

Cite your sources

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.

Point distribution

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

(No problem number 1, as usual)

The points assigned by the autograder based on syntax and docstring checking will be listed as problem 1 in Gradescope.

Problem 2: Argument with maximum digit sum

For the purposes of this problem, the digit sum of an integer is the sum of all of its decimal digits, e.g.

  • The digit sum of 1205 is 1+2+5 = 8
  • The digit sum of 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:

In [8]:
max_digit_sum(1205,99)  # 99 has largest digit sum 18
Out[8]:
99
In [9]:
max_digit_sum(156213)   # just returns the only argument given
Out[9]:
156213
In [10]:
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
Out[10]:
53

Problem 3: Decorator to require a return value

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:

In [6]:
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:

In [7]:
sum_plus_six(5,8)
Out[7]:
19
In [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.

In [2]:
@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:

In [24]:
sum_plus_six(5,8)
Out[24]:
19
In [ ]:
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!