diff --git a/nbgrader/docs/source/user_guide/source/ps1_autotest/jupyter.png b/nbgrader/docs/source/user_guide/source/ps1_autotest/jupyter.png new file mode 100644 index 000000000..201fc09ce Binary files /dev/null and b/nbgrader/docs/source/user_guide/source/ps1_autotest/jupyter.png differ diff --git a/nbgrader/docs/source/user_guide/source/ps1_autotest/problem1.ipynb b/nbgrader/docs/source/user_guide/source/ps1_autotest/problem1.ipynb new file mode 100644 index 000000000..16d713402 --- /dev/null +++ b/nbgrader/docs/source/user_guide/source/ps1_autotest/problem1.ipynb @@ -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 +} diff --git a/nbgrader/docs/source/user_guide/source/ps1_autotest/problem2.ipynb b/nbgrader/docs/source/user_guide/source/ps1_autotest/problem2.ipynb new file mode 100644 index 000000000..a8c653699 --- /dev/null +++ b/nbgrader/docs/source/user_guide/source/ps1_autotest/problem2.ipynb @@ -0,0 +1,79 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Consider the following piece of code:\n", + "\n", + "```python\n", + "def f(x):\n", + " if x == 0 or x == 1:\n", + " return x\n", + " return f(x - 1) + f(x - 2)\n", + "```" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "---\n", + "## Part A (1 point)\n", + "\n", + "Describe, in words, what this code does, and how it does it." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "nbgrader": { + "grade": true, + "grade_id": "part-a", + "locked": false, + "points": 1, + "schema_version": 3, + "solution": true + } + }, + "source": [ + "This function computes the fibonnaci sequence using recursion. The base cases are $x=0$ and $x=1$, in which case the function will return 0 or 1, respectively. In all other cases, the function will call itself to find the $x-1$ and $x-2$ fibonnaci numbers, and then add them together." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "---\n", + "## Part B (2 points)\n", + "\n", + "For what inputs will this function not behave as expected? What will happen?" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "nbgrader": { + "grade": true, + "grade_id": "part-b", + "locked": false, + "points": 2, + "schema_version": 3, + "solution": true + } + }, + "source": [ + "The function will not work correctly for inputs less than zero. Such inputs will result in an infinite recursion, as the function will keep subtracting one but never reach a base case that stops it." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python", + "language": "python", + "name": "python" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +}