Skip to content

Commit 9780d44

Browse files
import
1 parent fb8df1d commit 9780d44

10 files changed

+1572
-0
lines changed

.nbgrader.log

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[NbGraderApp | INFO] New config file saved to 'nbgrader_config.py'
2+
[AssignApp | INFO] Copying /Users/andrew/notebooks/courset_demo_1/source/./ps1/jupyter.png -> /Users/andrew/notebooks/courset_demo_1/release/./ps1/jupyter.png
3+
[AssignApp | INFO] Updating/creating assignment 'ps1': {}
4+
[AssignApp | INFO] Converting notebook /Users/andrew/notebooks/courset_demo_1/source/./ps1/problem1.ipynb
5+
[AssignApp | INFO] Writing 7884 bytes to /Users/andrew/notebooks/courset_demo_1/release/./ps1/problem1.ipynb
6+
[AssignApp | INFO] Converting notebook /Users/andrew/notebooks/courset_demo_1/source/./ps1/problem2.ipynb
7+
[AssignApp | INFO] Writing 2258 bytes to /Users/andrew/notebooks/courset_demo_1/release/./ps1/problem2.ipynb
8+
[AssignApp | INFO] Setting destination file permissions to 644

gradebook.db

132 KB
Binary file not shown.

nbgrader_config.py

+715
Large diffs are not rendered by default.

release/ps1/jupyter.png

5.6 KB
Loading

release/ps1/problem1.ipynb

+333
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,333 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"Before you turn this problem in, make sure everything runs as expected. First, **restart the kernel** (in the menubar, select Kernel$\\rightarrow$Restart) and then **run all cells** (in the menubar, select Cell$\\rightarrow$Run All).\n",
8+
"\n",
9+
"Make sure you fill in any place that says `YOUR CODE HERE` or \"YOUR ANSWER HERE\", as well as your name and collaborators below:"
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": null,
15+
"metadata": {},
16+
"outputs": [],
17+
"source": [
18+
"NAME = \"\"\n",
19+
"COLLABORATORS = \"\""
20+
]
21+
},
22+
{
23+
"cell_type": "markdown",
24+
"metadata": {},
25+
"source": [
26+
"---"
27+
]
28+
},
29+
{
30+
"cell_type": "markdown",
31+
"metadata": {
32+
"deletable": false,
33+
"editable": false,
34+
"nbgrader": {
35+
"checksum": "535c21960d4663d5edac398cb445d087",
36+
"grade": false,
37+
"grade_id": "jupyter",
38+
"locked": true,
39+
"schema_version": 1,
40+
"solution": false
41+
}
42+
},
43+
"source": [
44+
"For this problem set, we'll be using the Jupyter notebook:\n",
45+
"\n",
46+
"![](jupyter.png)"
47+
]
48+
},
49+
{
50+
"cell_type": "markdown",
51+
"metadata": {},
52+
"source": [
53+
"---\n",
54+
"## Part A (2 points)\n",
55+
"\n",
56+
"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`."
57+
]
58+
},
59+
{
60+
"cell_type": "code",
61+
"execution_count": null,
62+
"metadata": {
63+
"deletable": false,
64+
"nbgrader": {
65+
"checksum": "8f1eab8d02a9520920aa06f8a86a2492",
66+
"grade": false,
67+
"grade_id": "squares",
68+
"locked": false,
69+
"schema_version": 1,
70+
"solution": true
71+
}
72+
},
73+
"outputs": [],
74+
"source": [
75+
"def squares(n):\n",
76+
" \"\"\"Compute the squares of numbers from 1 to n, such that the \n",
77+
" ith element of the returned list equals i^2.\n",
78+
" \n",
79+
" \"\"\"\n",
80+
" # YOUR CODE HERE\n",
81+
" raise NotImplementedError()"
82+
]
83+
},
84+
{
85+
"cell_type": "markdown",
86+
"metadata": {},
87+
"source": [
88+
"Your function should print `[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]` for $n=10$. Check that it does:"
89+
]
90+
},
91+
{
92+
"cell_type": "code",
93+
"execution_count": null,
94+
"metadata": {},
95+
"outputs": [],
96+
"source": [
97+
"squares(10)"
98+
]
99+
},
100+
{
101+
"cell_type": "code",
102+
"execution_count": null,
103+
"metadata": {
104+
"deletable": false,
105+
"editable": false,
106+
"nbgrader": {
107+
"checksum": "8f41dd0f9c8fd2da8e8708d73e506b3a",
108+
"grade": true,
109+
"grade_id": "correct_squares",
110+
"locked": false,
111+
"points": 1.0,
112+
"schema_version": 1,
113+
"solution": false
114+
}
115+
},
116+
"outputs": [],
117+
"source": [
118+
"\"\"\"Check that squares returns the correct output for several inputs\"\"\"\n",
119+
"assert squares(1) == [1]\n",
120+
"assert squares(2) == [1, 4]\n",
121+
"assert squares(10) == [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n",
122+
"assert squares(11) == [1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121]"
123+
]
124+
},
125+
{
126+
"cell_type": "code",
127+
"execution_count": null,
128+
"metadata": {
129+
"deletable": false,
130+
"editable": false,
131+
"nbgrader": {
132+
"checksum": "23c2b667d3b60eff3be46eb3290a6b4a",
133+
"grade": true,
134+
"grade_id": "squares_invalid_input",
135+
"locked": false,
136+
"points": 1.0,
137+
"schema_version": 1,
138+
"solution": false
139+
}
140+
},
141+
"outputs": [],
142+
"source": [
143+
"\"\"\"Check that squares raises an error for invalid inputs\"\"\"\n",
144+
"try:\n",
145+
" squares(0)\n",
146+
"except ValueError:\n",
147+
" pass\n",
148+
"else:\n",
149+
" raise AssertionError(\"did not raise\")\n",
150+
"\n",
151+
"try:\n",
152+
" squares(-4)\n",
153+
"except ValueError:\n",
154+
" pass\n",
155+
"else:\n",
156+
" raise AssertionError(\"did not raise\")"
157+
]
158+
},
159+
{
160+
"cell_type": "markdown",
161+
"metadata": {},
162+
"source": [
163+
"---\n",
164+
"\n",
165+
"## Part B (1 point)\n",
166+
"\n",
167+
"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."
168+
]
169+
},
170+
{
171+
"cell_type": "code",
172+
"execution_count": null,
173+
"metadata": {
174+
"deletable": false,
175+
"nbgrader": {
176+
"checksum": "166e1abc6621f93f8084ec312c49f757",
177+
"grade": false,
178+
"grade_id": "sum_of_squares",
179+
"locked": false,
180+
"schema_version": 1,
181+
"solution": true
182+
}
183+
},
184+
"outputs": [],
185+
"source": [
186+
"def sum_of_squares(n):\n",
187+
" \"\"\"Compute the sum of the squares of numbers from 1 to n.\"\"\"\n",
188+
" # YOUR CODE HERE\n",
189+
" raise NotImplementedError()"
190+
]
191+
},
192+
{
193+
"cell_type": "markdown",
194+
"metadata": {},
195+
"source": [
196+
"The sum of squares from 1 to 10 should be 385. Verify that this is the answer you get:"
197+
]
198+
},
199+
{
200+
"cell_type": "code",
201+
"execution_count": null,
202+
"metadata": {},
203+
"outputs": [],
204+
"source": [
205+
"sum_of_squares(10)"
206+
]
207+
},
208+
{
209+
"cell_type": "code",
210+
"execution_count": null,
211+
"metadata": {
212+
"deletable": false,
213+
"editable": false,
214+
"nbgrader": {
215+
"checksum": "35f884697f5b0145e7306523a083cfd1",
216+
"grade": true,
217+
"grade_id": "correct_sum_of_squares",
218+
"locked": false,
219+
"points": 0.5,
220+
"schema_version": 1,
221+
"solution": false
222+
}
223+
},
224+
"outputs": [],
225+
"source": [
226+
"\"\"\"Check that sum_of_squares returns the correct answer for various inputs.\"\"\"\n",
227+
"assert sum_of_squares(1) == 1\n",
228+
"assert sum_of_squares(2) == 5\n",
229+
"assert sum_of_squares(10) == 385\n",
230+
"assert sum_of_squares(11) == 506"
231+
]
232+
},
233+
{
234+
"cell_type": "code",
235+
"execution_count": null,
236+
"metadata": {
237+
"deletable": false,
238+
"editable": false,
239+
"nbgrader": {
240+
"checksum": "2d414bfb292d5a6d2b442fc7d8206a0b",
241+
"grade": true,
242+
"grade_id": "sum_of_squares_uses_squares",
243+
"locked": false,
244+
"points": 0.5,
245+
"schema_version": 1,
246+
"solution": false
247+
}
248+
},
249+
"outputs": [],
250+
"source": [
251+
"\"\"\"Check that sum_of_squares relies on squares.\"\"\"\n",
252+
"orig_squares = squares\n",
253+
"del squares\n",
254+
"try:\n",
255+
" sum_of_squares(1)\n",
256+
"except NameError:\n",
257+
" pass\n",
258+
"else:\n",
259+
" raise AssertionError(\"sum_of_squares does not use squares\")\n",
260+
"finally:\n",
261+
" squares = orig_squares"
262+
]
263+
},
264+
{
265+
"cell_type": "markdown",
266+
"metadata": {},
267+
"source": [
268+
"---\n",
269+
"## Part C (1 point)\n",
270+
"\n",
271+
"Using LaTeX math notation, write out the equation that is implemented by your `sum_of_squares` function."
272+
]
273+
},
274+
{
275+
"cell_type": "markdown",
276+
"metadata": {
277+
"deletable": false,
278+
"nbgrader": {
279+
"checksum": "f3cc38a3e522c0be10852ebbe2a638b7",
280+
"grade": true,
281+
"grade_id": "sum_of_squares_equation",
282+
"locked": false,
283+
"points": 1.0,
284+
"schema_version": 1,
285+
"solution": true
286+
}
287+
},
288+
"source": [
289+
"YOUR ANSWER HERE"
290+
]
291+
},
292+
{
293+
"cell_type": "markdown",
294+
"metadata": {},
295+
"source": [
296+
"---\n",
297+
"## Part D (2 points)\n",
298+
"\n",
299+
"Find a usecase for your `sum_of_squares` function and implement that usecase in the cell below."
300+
]
301+
},
302+
{
303+
"cell_type": "code",
304+
"execution_count": null,
305+
"metadata": {
306+
"deletable": false,
307+
"nbgrader": {
308+
"checksum": "5a96910dbc324f5565edf92f5c98af1b",
309+
"grade": true,
310+
"grade_id": "sum_of_squares_application",
311+
"locked": false,
312+
"points": 2.0,
313+
"schema_version": 1,
314+
"solution": true
315+
}
316+
},
317+
"outputs": [],
318+
"source": [
319+
"# YOUR CODE HERE\n",
320+
"raise NotImplementedError()"
321+
]
322+
}
323+
],
324+
"metadata": {
325+
"kernelspec": {
326+
"display_name": "Python",
327+
"language": "python",
328+
"name": "python"
329+
}
330+
},
331+
"nbformat": 4,
332+
"nbformat_minor": 0
333+
}

0 commit comments

Comments
 (0)