Skip to content

Commit efece30

Browse files
committed
2 parents 3225b71 + f26de9f commit efece30

5 files changed

Lines changed: 92 additions & 798 deletions

File tree

Week_10_classes/lec_act_10_class_pinball_wall.ipynb renamed to Week_10_classes/lec_act_10_classes.ipynb

Lines changed: 85 additions & 164 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,18 @@
1111
"source": [
1212
"# Initialize Otter\n",
1313
"import otter\n",
14-
"grader = otter.Notebook(\"lec_act_10_class_pinball_wall.ipynb\")"
14+
"grader = otter.Notebook(\"lec_act_10_classes.ipynb\")"
1515
]
1616
},
1717
{
1818
"cell_type": "markdown",
1919
"metadata": {},
2020
"source": [
21-
"# Creating a pinball wall class\n",
21+
"# Creating a ground segment class\n",
2222
"\n",
23-
"Extend the wall class from the tutorial to do two new things\n",
24-
"- Reflect off of the wall [vertical/horizontal, general is optional]\n",
25-
"- Plot self\n",
23+
"This is mostly filling in the methods in the class\n",
2624
"\n",
27-
"The code is in **pinball_wall.py**"
25+
"You will have to decide what variables you want to keep in the class"
2826
]
2927
},
3028
{
@@ -35,37 +33,24 @@
3533
"source": [
3634
"# Doing the imports for you\n",
3735
"import numpy as np\n",
38-
"import matplotlib.pyplot as plt"
39-
]
40-
},
41-
{
42-
"cell_type": "code",
43-
"execution_count": null,
44-
"metadata": {},
45-
"outputs": [],
46-
"source": [
47-
"# These commands will force JN to actually re-load the external file when you re-execute the import command\n",
48-
"%load_ext autoreload\n",
49-
"%autoreload 2"
36+
"import matplotlib.pyplot as plt\n",
37+
"from numpy.polynomial import polynomial\n"
5038
]
5139
},
5240
{
5341
"cell_type": "markdown",
54-
"metadata": {
55-
"deletable": false,
56-
"editable": false
57-
},
42+
"metadata": {},
5843
"source": [
59-
"# Add reflect to wall code\n",
44+
"# Ground segment class\n",
45+
"\n",
46+
"Note, this is a simplified version of the one in ski_jump.py\n",
6047
"\n",
61-
"In this problem, fill in **reflect** method in **PinballWall** in **pinball_wall.py**.\n",
48+
"In this problem, fill in **init** method and **plot** \n",
6249
"\n",
63-
"You're welcome to create more class variables in the **__init__** function or add more methods\n",
50+
"You're welcome to create more variables in the **__init__** function or add more methods to the class\n",
6451
"\n",
6552
"TODO: \n",
66-
"- reflect (this question)\n",
67-
"- test_reflect (next question)\n",
68-
"- plot (last question)"
53+
"- Fill in all of the methods in the class"
6954
]
7055
},
7156
{
@@ -76,53 +61,34 @@
7661
},
7762
"outputs": [],
7863
"source": [
79-
"# You can import classes just like you import functions\n",
80-
"from pinball_wall import PinballWall"
81-
]
82-
},
83-
{
84-
"cell_type": "code",
85-
"execution_count": null,
86-
"metadata": {
87-
"tags": []
88-
},
89-
"outputs": [],
90-
"source": [
91-
"# Create instances and call test functions\n",
92-
"# Now create two instances of the class\n",
93-
"# Notice that we do NOT pass in a self variable\n",
94-
"my_vert_wall = PinballWall(wall_type=\"Vertical\", intercept_value=.2)\n",
95-
"my_horiz_wall = PinballWall(wall_type=\"Horizontal\", intercept_value=.2)\n",
64+
"class SimpleGroundSection:\n",
65+
" def __init__(self, x_start, x_end, polynomial_coefs=(0, 0)):\n",
66+
" \"\"\"\n",
67+
" @param x_start What x value to start at\n",
68+
" @param x_end What x value to end at\n",
69+
" @param polynomial coeficients for the section y(x) = a0 x^2 + a1 x + a2\n",
70+
" \"\"\"\n",
71+
" # TODO - store the start and stop values along with the coefficients\n",
72+
" ...\n",
9673
"\n",
97-
"# Print them both - this will call the string function\n",
98-
"print(f\"Vertical wall: {my_vert_wall}\")\n",
99-
"print(f\"Horizontal wall: {my_horiz_wall}\")\n",
74+
" def is_between(self, x):\n",
75+
" \"\"\" Is the x value between the start and end x value?\n",
76+
" @param x the x value \n",
77+
" @ return True or False\n",
78+
" \"\"\"\n",
79+
" ...\n",
10080
"\n",
101-
"# TODO check your reflect function here"
102-
]
103-
},
104-
{
105-
"cell_type": "code",
106-
"execution_count": null,
107-
"metadata": {
108-
"deletable": false,
109-
"editable": false
110-
},
111-
"outputs": [],
112-
"source": [
113-
"grader.check(\"class_defn\")"
114-
]
115-
},
116-
{
117-
"cell_type": "markdown",
118-
"metadata": {
119-
"deletable": false,
120-
"editable": false
121-
},
122-
"source": [
123-
"# Write a test reflect function\n",
81+
" def height(self, x):\n",
82+
" \"\"\" Return the height, given the x (assumes over the ground)\n",
83+
" @param x - x value\n",
84+
" @returns height f(x)\"\"\"\n",
85+
" ...\n",
86+
" \n",
87+
" def plot(self, axs):\n",
88+
" \"\"\"Plot the section\n",
89+
" @param axs the figure to plot into\"\"\"\n",
12490
"\n",
125-
"TODO: Create some tests in the **test_reflect** method. "
91+
" ..."
12692
]
12793
},
12894
{
@@ -133,34 +99,21 @@
13399
},
134100
"outputs": [],
135101
"source": [
136-
"# Call the test function\n",
137-
"assert my_vert_wall.test_reflect()"
138-
]
139-
},
140-
{
141-
"cell_type": "code",
142-
"execution_count": null,
143-
"metadata": {
144-
"deletable": false,
145-
"editable": false
146-
},
147-
"outputs": [],
148-
"source": [
149-
"grader.check(\"test_reflect\")"
150-
]
151-
},
152-
{
153-
"cell_type": "markdown",
154-
"metadata": {
155-
"deletable": false,
156-
"editable": false
157-
},
158-
"source": [
159-
"# Fill in the plot function \n",
102+
"# Create instances and call test functions\n",
160103
"\n",
161-
"TODO: Fill in the plot function\n",
104+
"# Create and test a single, horizontal linear segment at height 1\n",
105+
"# polynomial is p0 + p1 x + p2 x^2... etc\n",
106+
"flat_bit = SimpleGroundSection(-0.5, 1.0, polynomial_coefs=(1, 0))\n",
107+
"assert not flat_bit.is_between(-0.6)\n",
108+
"assert flat_bit.is_between(-0.5)\n",
109+
"assert flat_bit.is_between(0.0) \n",
110+
"assert flat_bit.is_between(1.0) \n",
111+
"assert not flat_bit.is_between(1.1)\n",
112+
"assert flat_bit.is_over(0.1, 1.2)\n",
113+
"assert np.isclose(flat_bit.height(0.0), 1.0)\n",
162114
"\n",
163-
"Slanted lines are optional for this assignment"
115+
"fig, axs = plt.subplots(1, 1, figsize=(2, 2))\n",
116+
"flat_bit.plot(axs)\n"
164117
]
165118
},
166119
{
@@ -171,28 +124,20 @@
171124
},
172125
"outputs": [],
173126
"source": [
174-
"def plot_check(axs):\n",
175-
" # The values we calculated in calculate_n_time_steps\n",
176-
" left = -4.0\n",
177-
" right = 4.0\n",
178-
" height = 10.0\n",
179-
"\n",
180-
" axs.plot([left, right], [height, height], color='grey', linestyle=\"dotted\")\n",
181-
" axs.plot([left, right], [0, 0], color='grey', linestyle=\"dotted\")\n",
182-
" axs.plot([left, left], [0, height], color='grey', linestyle=\"dotted\")\n",
183-
" axs.plot([right, right], [0, height], color='grey', linestyle=\"dotted\")\n",
184127
"\n",
185-
" walls = []\n",
186-
" walls.append(PinballWall(\"Vertical\", -3.0))\n",
187-
" walls.append(PinballWall(\"Vertical\", 3.0))\n",
188-
" walls.append(PinballWall(\"Horizontal\", 8.0))\n",
189-
" walls.append(PinballWall(\"General\", a_b_c=[0.5, 0.5, 1.0]))\n",
190-
" walls.append(PinballWall(\"General\", a_b_c=[-0.5, 0.5, 1.0]))\n",
191-
" for w in walls:\n",
192-
" w.plot(axs, left, right, height)\n",
128+
"# Create and test a slightly descending straight line\n",
129+
"# Starts at 1 and drops by 0.25\n",
130+
"slope_down_bit = SimpleGroundSection(1.0, 1.5, polynomial_coefs=(1.0 + 0.25, -0.25))\n",
131+
"assert not slope_down_bit.is_between(0.9)\n",
132+
"assert slope_down_bit.is_between(1.0)\n",
133+
"assert slope_down_bit.is_between(1.25) \n",
134+
"assert slope_down_bit.is_between(1.5) \n",
135+
"assert not slope_down_bit.is_between(1.6)\n",
136+
"assert slope_down_bit.is_over(1.25, 1.0)\n",
137+
"assert np.isclose(slope_down_bit.height(1.0), 1.0)\n",
193138
"\n",
194-
" axs.axis('equal')\n",
195-
" axs.set_title(f\"Pinball walls\")"
139+
"fig, axs = plt.subplots(1, 1, figsize=(2, 2))\n",
140+
"slope_down_bit.plot(axs)\n"
196141
]
197142
},
198143
{
@@ -203,11 +148,20 @@
203148
},
204149
"outputs": [],
205150
"source": [
206-
"nrows = 1\n",
207-
"ncols = 1\n",
208-
"fig, axs = plt.subplots(nrows, ncols, figsize=(4, 4))\n",
209151
"\n",
210-
"plot_check(axs)"
152+
"# Create and test a parabola\n",
153+
"# Interpolates the points (0, 2), (1, 1), (3, 2)\n",
154+
"parabola = SimpleGroundSection(0.0, 2.0, polynomial_coefs=(2.0, -3.0/2.0, 1.0/2.0))\n",
155+
"assert not parabola.is_between(-0.1)\n",
156+
"assert parabola.is_between(1.0)\n",
157+
"assert not slope_down_bit.is_between(2.1)\n",
158+
"assert parabola.is_over(1.0, 1.5)\n",
159+
"assert np.isclose(parabola.height(0.0), 2.0)\n",
160+
"assert np.isclose(parabola.height(3.0), 2.0)\n",
161+
"\n",
162+
"\n",
163+
"fig, axs = plt.subplots(1, 1, figsize=(2, 2))\n",
164+
"parabola.plot(axs)\n"
211165
]
212166
},
213167
{
@@ -219,7 +173,7 @@
219173
},
220174
"outputs": [],
221175
"source": [
222-
"grader.check(\"Plot_wall\")"
176+
"grader.check(\"class_defn\")"
223177
]
224178
},
225179
{
@@ -299,43 +253,29 @@
299253
"name": "python",
300254
"nbconvert_exporter": "python",
301255
"pygments_lexer": "ipython3",
302-
"version": "3.11.7"
256+
"version": "3.12.7"
303257
},
304258
"otter": {
305259
"OK_FORMAT": true,
306260
"tests": {
307-
"Plot_wall": {
308-
"name": "Plot_wall",
309-
"points": 1,
310-
"suites": [
311-
{
312-
"cases": [
313-
{
314-
"code": ">>> plot_check(axs)\n",
315-
"hidden": false,
316-
"locked": false
317-
}
318-
],
319-
"scored": true,
320-
"setup": "",
321-
"teardown": "",
322-
"type": "doctest"
323-
}
324-
]
325-
},
326261
"class_defn": {
327262
"name": "class_defn",
328263
"points": 2,
329264
"suites": [
330265
{
331266
"cases": [
332267
{
333-
"code": ">>> assert my_vert_wall.inside(my_vert_wall.reflect([0.25, 0.0], [1, 0])[0])\n",
268+
"code": ">>> assert not flat_bit.is_between(-0.6)\n>>> assert flat_bit.is_between(-0.5)\n>>> assert flat_bit.is_between(0.0)\n>>> assert flat_bit.is_between(1.0)\n>>> assert not flat_bit.is_between(1.1)\n>>> assert flat_bit.is_over(0.1, 1.2)\n>>> assert np.isclose(flat_bit.height(0.0), 1.0)\n",
334269
"hidden": false,
335270
"locked": false
336271
},
337272
{
338-
"code": ">>> assert my_vert_wall.reflect([0.25, 0.0], [1, 0])[1][0] < 0\n",
273+
"code": ">>> assert not slope_down_bit.is_between(0.9)\n>>> assert slope_down_bit.is_between(1.0)\n>>> assert slope_down_bit.is_between(1.25)\n>>> assert slope_down_bit.is_between(1.5)\n>>> assert not slope_down_bit.is_between(1.6)\n>>> assert slope_down_bit.is_over(1.25, 1.0)\n>>> assert np.isclose(slope_down_bit.height(1.0), 1.0)\n",
274+
"hidden": false,
275+
"locked": false
276+
},
277+
{
278+
"code": ">>> assert not parabola.is_between(-0.1)\n>>> assert parabola.is_between(1.0)\n>>> assert not slope_down_bit.is_between(2.1)\n>>> assert parabola.is_over(1.0, 1.5)\n>>> assert np.isclose(parabola.height(0.0), 2.0)\n>>> assert np.isclose(parabola.height(3.0), 2.0)\n",
339279
"hidden": false,
340280
"locked": false
341281
}
@@ -375,25 +315,6 @@
375315
"type": "doctest"
376316
}
377317
]
378-
},
379-
"test_reflect": {
380-
"name": "test_reflect",
381-
"points": 2,
382-
"suites": [
383-
{
384-
"cases": [
385-
{
386-
"code": ">>> assert my_vert_wall.test_reflect()\n",
387-
"hidden": false,
388-
"locked": false
389-
}
390-
],
391-
"scored": true,
392-
"setup": "",
393-
"teardown": "",
394-
"type": "doctest"
395-
}
396-
]
397318
}
398319
}
399320
}

0 commit comments

Comments
 (0)