forked from jupyter/nbgrader
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
moved problem3 to its own ps1_autotest assignment
- Loading branch information
1 parent
a881d2b
commit 1af6da2
Showing
3 changed files
with
478 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
399 changes: 399 additions & 0 deletions
399
nbgrader/docs/source/user_guide/source/ps1_autotest/problem1.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,399 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"nbgrader": { | ||
"grade": false, | ||
"grade_id": "jupyter", | ||
"locked": true, | ||
"schema_version": 3, | ||
"solution": false | ||
} | ||
}, | ||
"source": [ | ||
"For this problem set, we'll be using the Jupyter notebook:\n", | ||
"\n", | ||
"![](jupyter.png)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"---\n", | ||
"## Part A (2 points)\n", | ||
"\n", | ||
"Write a function that returns a list of numbers, such that $x_i=i^2$, for $1\\leq i \\leq n$. Make sure it handles the case where $n<1$ by raising a `ValueError`." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"nbgrader": { | ||
"grade": false, | ||
"grade_id": "squares", | ||
"locked": false, | ||
"schema_version": 3, | ||
"solution": true | ||
}, | ||
"vscode": { | ||
"languageId": "python" | ||
} | ||
}, | ||
"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", | ||
" ### BEGIN SOLUTION\n", | ||
" if n < 1:\n", | ||
" raise ValueError(\"n must be greater than or equal to 1\")\n", | ||
" return [i ** 2 for i in range(1, n + 1)]\n", | ||
" ### END SOLUTION" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"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": { | ||
"vscode": { | ||
"languageId": "python" | ||
} | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"squares(10)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"nbgrader": { | ||
"grade": true, | ||
"grade_id": "correct_squares", | ||
"locked": false, | ||
"points": 1, | ||
"schema_version": 3, | ||
"solution": false | ||
}, | ||
"vscode": { | ||
"languageId": "python" | ||
} | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"\"\"\"Check that squares returns the correct output for several inputs\"\"\"\n", | ||
"### AUTOTEST squares(1); squares(2)\n", | ||
"### HASHED AUTOTEST squares(3)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"nbgrader": { | ||
"grade": true, | ||
"grade_id": "squares_invalid_input", | ||
"locked": false, | ||
"points": 1, | ||
"schema_version": 3, | ||
"solution": false | ||
}, | ||
"vscode": { | ||
"languageId": "python" | ||
} | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"\"\"\"Check that squares raises an error for invalid inputs\"\"\"\n", | ||
"def test_func_throws(func, ErrorType):\n", | ||
" try:\n", | ||
" func()\n", | ||
" except ErrorType:\n", | ||
" return True\n", | ||
" else:\n", | ||
" print('Did not raise right type of error!')\n", | ||
" return False\n", | ||
" \n", | ||
"### AUTOTEST test_func_throws(lambda : squares(0), ValueError)\n", | ||
"### AUTOTEST test_func_throws(lambda : squares(-4), ValueError);\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"---\n", | ||
"\n", | ||
"## Part B (1 point)\n", | ||
"\n", | ||
"Using your `squares` function, write a function that computes 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." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"nbgrader": { | ||
"grade": false, | ||
"grade_id": "sum_of_squares", | ||
"locked": false, | ||
"schema_version": 3, | ||
"solution": true | ||
}, | ||
"vscode": { | ||
"languageId": "python" | ||
} | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"def sum_of_squares(n):\n", | ||
" \"\"\"Compute the sum of the squares of numbers from 1 to n.\"\"\"\n", | ||
" ### BEGIN SOLUTION\n", | ||
" return sum(squares(n))\n", | ||
" ### END SOLUTION" | ||
] | ||
}, | ||
{ | ||
"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": { | ||
"vscode": { | ||
"languageId": "python" | ||
} | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"sum_of_squares(10)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"nbgrader": { | ||
"grade": true, | ||
"grade_id": "correct_sum_of_squares", | ||
"locked": false, | ||
"points": 0.5, | ||
"schema_version": 3, | ||
"solution": false | ||
}, | ||
"vscode": { | ||
"languageId": "python" | ||
} | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"\"\"\"Check that sum_of_squares returns the correct answer for various inputs.\"\"\"\n", | ||
"### AUTOTEST sum_of_squares(1)\n", | ||
"### AUTOTEST sum_of_squares(2); sum_of_squares(10) \n", | ||
"### AUTOTEST sum_of_squares(11) \n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"nbgrader": { | ||
"grade": true, | ||
"grade_id": "sum_of_squares_uses_squares", | ||
"locked": false, | ||
"points": 0.5, | ||
"schema_version": 3, | ||
"solution": false | ||
}, | ||
"vscode": { | ||
"languageId": "python" | ||
} | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"\"\"\"Check that sum_of_squares relies on squares.\"\"\"\n", | ||
"\n", | ||
"orig_squares = squares\n", | ||
"del squares\n", | ||
"\n", | ||
"### AUTOTEST test_func_throws(lambda : sum_of_squares(1), NameError)\n", | ||
"\n", | ||
"squares = orig_squares\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"---\n", | ||
"## Part C (1 point)\n", | ||
"\n", | ||
"Using LaTeX math notation, write out the equation that is implemented by your `sum_of_squares` function." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"nbgrader": { | ||
"grade": true, | ||
"grade_id": "sum_of_squares_equation", | ||
"locked": false, | ||
"points": 1, | ||
"schema_version": 3, | ||
"solution": true | ||
} | ||
}, | ||
"source": [ | ||
"$\\sum_{i=1}^n i^2$" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"---\n", | ||
"## Part D (2 points)\n", | ||
"\n", | ||
"Find a usecase for your `sum_of_squares` function and implement that usecase in the cell below." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"nbgrader": { | ||
"grade": true, | ||
"grade_id": "sum_of_squares_application", | ||
"locked": false, | ||
"points": 2, | ||
"schema_version": 3, | ||
"solution": true | ||
}, | ||
"vscode": { | ||
"languageId": "python" | ||
} | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"def pyramidal_number(n):\n", | ||
" \"\"\"Returns the n^th pyramidal number\"\"\"\n", | ||
" return sum_of_squares(n)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"nbgrader": { | ||
"grade": false, | ||
"grade_id": "cell-938593c4a215c6cc", | ||
"locked": true, | ||
"points": 4, | ||
"schema_version": 3, | ||
"solution": false, | ||
"task": true | ||
} | ||
}, | ||
"source": [ | ||
"---\n", | ||
"## Part E (4 points)\n", | ||
"\n", | ||
"State the formulae for an arithmetic and geometric sum and verify them numerically for an example of your choice." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"---\n", | ||
"## Part F (1 points)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"nbgrader": { | ||
"grade": false, | ||
"grade_id": "cell-d3df8cd59fd0eb74", | ||
"locked": false, | ||
"schema_version": 3, | ||
"solution": true, | ||
"task": false | ||
}, | ||
"vscode": { | ||
"languageId": "python" | ||
} | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"my_dictionary = {\n", | ||
" 'one' : 1,\n", | ||
" 'two' : 2,\n", | ||
" 'three' : 3\n", | ||
"}" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"nbgrader": { | ||
"grade": true, | ||
"grade_id": "cell-6e9ff83aa5dfaf17", | ||
"locked": true, | ||
"points": 0, | ||
"schema_version": 3, | ||
"solution": false, | ||
"task": false | ||
}, | ||
"vscode": { | ||
"languageId": "python" | ||
} | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"### AUTOTEST my_dictionary\n", | ||
"### AUTOTEST my_dictionary[\"one\"]" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"celltoolbar": "Create Assignment", | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"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.9.12" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 1 | ||
} |
Oops, something went wrong.