{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Denison CS181/DA210 Homework\n",
"\n",
"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).\n",
"\n",
"Make sure you fill in any place that says `YOUR CODE HERE` or \"YOUR ANSWER HERE\"."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Instructions and Preliminary Cells\n",
"\n",
"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."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os.path\n",
"\n",
"datadir = None"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Problems"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"\n",
"**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 \n",
"```\n",
" # YOUR CODE HERE\n",
" raise NotImplementedError()\n",
"```\n",
"You will **delete both of these lines** and replace them with your solution to the problem, properly indented as the body of a function.\n",
"\n",
"Here is actual wording of the problem:\n",
"\n",
"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."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "8e77664d83e457f1abe97f1a0675559e",
"grade": false,
"grade_id": "cell-42799b16e99f7c0d",
"locked": false,
"schema_version": 3,
"solution": true,
"task": false
}
},
"outputs": [],
"source": [
"def squares(n):\n",
" \"\"\"Compute the squares of numbers from 1 to n, such that the \n",
" ith element of the returned list equals i^2.\n",
" \n",
" \"\"\"\n",
" # YOUR CODE HERE\n",
" raise NotImplementedError()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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.\n",
"\n",
"Your function should print `[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]` for $n=10$. Check that it does:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"squares(10)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "460d808c2b6506a6074e3309f2c8b295",
"grade": true,
"grade_id": "cell-6064da463ce530d8",
"locked": true,
"points": 1,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"\"\"\"Check that squares returns the correct output for several inputs\"\"\"\n",
"assert squares(1) == [1]\n",
"assert squares(2) == [1, 4]\n",
"assert squares(10) == [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n",
"assert squares(11) == [1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "f82959598e5908b33e7dcf49a9a17f50",
"grade": true,
"grade_id": "cell-9c9e80eda32a70a4",
"locked": true,
"points": 1,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"\"\"\"Check that squares gives an empty list for invalid inputs\"\"\"\n",
"assert squares(0) == []\n",
"assert squares(-5) == []\n",
"assert squares(\"foo\") == []"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"\n",
"\n",
"**Q2** Write a function\n",
"\n",
" sum_of_sqaures(n)\n",
" \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.\n",
"\n",
"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."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "6fc4e0235b59f95ac714d8ea2aa2fdf2",
"grade": false,
"grade_id": "cell-86fdbeebcc5eb486",
"locked": false,
"schema_version": 3,
"solution": true,
"task": false
}
},
"outputs": [],
"source": [
"# YOUR CODE HERE\n",
"raise NotImplementedError()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The sum of squares from 1 to 10 should be 385. Verify that this is the answer you get:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sum_of_squares(10)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "aac4a804ec71ce38ec6c6dcfed71997c",
"grade": true,
"grade_id": "cell-a31e9740d73b42f2",
"locked": true,
"points": 0.5,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"\"\"\"Check that sum_of_squares returns the correct answer for various inputs.\"\"\"\n",
"assert sum_of_squares(1) == 1\n",
"assert sum_of_squares(2) == 5\n",
"assert sum_of_squares(10) == 385\n",
"assert sum_of_squares(11) == 506"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "1ff399942a3c929e3d995c0fd5961f62",
"grade": true,
"grade_id": "cell-1086af5a64b556a6",
"locked": true,
"points": 0.5,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"\"\"\"Check that sum_of_squares relies on squares.\"\"\"\n",
"orig_squares = squares\n",
"del squares\n",
"try:\n",
" sum_of_squares(1)\n",
"except NameError:\n",
" pass\n",
"else:\n",
" raise AssertionError(\"sum_of_squares does not use squares\")\n",
"finally:\n",
" squares = orig_squares"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}