Denison CS181/DA210 Homework

Before you turn this problem in, make sure everything runs as expected. This is a combination of restarting the kernel and then running all cells (in the menubar, select Kernel$\rightarrow$Restart And Run All).

Make sure you fill in any place that says YOUR CODE HERE or "YOUR ANSWER HERE".


Instructions and Preliminary Cells

For many homework notebooks, there well be a prolog that contains a code cell containing setup code, like imports, global variable assignments, and utility functions. You should execute this code cell prior to writing any solution code.

In [ ]:
import os.path

datadir = None

Problems


Q1 This is an example homework problem that asks you to write a (simple) function based on a specification. In this example, the code cell that follows "gets you started" by writing the definition line of the function and a minimal docstring. Indented within the body of this starter code, you see

    # YOUR CODE HERE
    raise NotImplementedError()

You will delete both of these lines and replace them with your solution to the problem, properly indented as the body of a function.

Here is actual wording of the problem:

Write a function that returns a list of numbers, such that $x_i=i^2$, for $1\leq i \leq n$, returning the resultant list. If $n$ is 0, negative, or not an integer, return an empty list.

In [ ]:
def squares(n):
    """Compute the squares of numbers from 1 to n, such that the 
    ith element of the returned list equals i^2.
    
    """
    # YOUR CODE HERE
    raise NotImplementedError()

In some cases, the homework problem may, in either the solution cell or in a following cell, specify an example or two of correct operation, and perhaps code cell(s) that show invocation, like we do here. You should feel free to add your own code cells that you use to test and debug your solution. Such additions will not impact the grading of the notebook.

Your function should print [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] for $n=10$. Check that it does:

In [ ]:
squares(10)

For many coding problems, there will be cells that check your work, both to give you feedback on whether or not you are on the right track, and to support grading of your assignments. In the cell that follows, your solution is tested by invoking the function you wrote and comparing your results with a correct expected result. The assert causes a visible exception/error when your solution and the expected correct result do not match. But if they do match the cell executes without error, and without any visible output. You should run these cells to check your work.

In [ ]:
"""Check that squares returns the correct output for several inputs"""
assert squares(1) == [1]
assert squares(2) == [1, 4]
assert squares(10) == [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
assert squares(11) == [1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121]

For the purpose of determining the degree to which you have correctly solved a problem, there may be additional assert cells with additional tests. For a problem, there may also be cells that contain "hidden" tests where the instructor version has these additional tests, while the student version does not show the extra tests. The grading process will be based on both the visible and the hidden tests.

In [ ]:
"""Check that squares gives an empty list for invalid inputs"""
assert squares(0) == []
assert squares(-5) == []
assert squares("foo") == []

Q2 Write a function

sum_of_sqaures(n)

That uses your squares function, to compute the sum of the squares of the numbers from 1 to $n$. Your function should call the squares function -- it should NOT reimplement its functionality.

In this case, the code cell below for your solution does not have code to help you get started. You are expected to provide the function defition line, the docstring, and the function body code to implement the complete solution.

In [ ]:
# YOUR CODE HERE
raise NotImplementedError()

The sum of squares from 1 to 10 should be 385. Verify that this is the answer you get:

In [ ]:
sum_of_squares(10)
In [ ]:
"""Check that sum_of_squares returns the correct answer for various inputs."""
assert sum_of_squares(1) == 1
assert sum_of_squares(2) == 5
assert sum_of_squares(10) == 385
assert sum_of_squares(11) == 506
In [ ]:
"""Check that sum_of_squares relies on squares."""
orig_squares = squares
del squares
try:
    sum_of_squares(1)
except NameError:
    pass
else:
    raise AssertionError("sum_of_squares does not use squares")
finally:
    squares = orig_squares