Skip to content

Commit b164cd8

Browse files
committed
fixed Part 2 - Project 2
1 parent d0ba910 commit b164cd8

File tree

3 files changed

+83
-25
lines changed

3 files changed

+83
-25
lines changed

Part 2/Section 05 - Project 2/01 - Project - Description.ipynb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,48 @@
205205
" assert p1 != p4\n",
206206
" assert p4 == p5"
207207
]
208+
},
209+
{
210+
"cell_type": "code",
211+
"execution_count": 1,
212+
"metadata": {
213+
"collapsed": true
214+
},
215+
"outputs": [],
216+
"source": [
217+
"class Polygons:\n",
218+
" def __init__(self, m, R):\n",
219+
" if m < 3:\n",
220+
" raise ValueError('m must be greater than 3')\n",
221+
" self._m = m\n",
222+
" self._R = R\n",
223+
" self._polygons = [Polygon(i, R) for i in range(3, m+1)]\n",
224+
" \n",
225+
" def __len__(self):\n",
226+
" return self._m - 2\n",
227+
" \n",
228+
" def __repr__(self):\n",
229+
" return f'Polygons(m={self._m}, R={self._R})'\n",
230+
" \n",
231+
" def __getitem__(self, s):\n",
232+
" return self._polygons[s]\n",
233+
" \n",
234+
" @property\n",
235+
" def max_efficiency_polygon(self):\n",
236+
" sorted_polygons = sorted(self._polygons, \n",
237+
" key=lambda p: p.area/p.perimeter,\n",
238+
" reverse=True)\n",
239+
" return sorted_polygons[0]"
240+
]
241+
},
242+
{
243+
"cell_type": "code",
244+
"execution_count": null,
245+
"metadata": {
246+
"collapsed": true
247+
},
248+
"outputs": [],
249+
"source": []
208250
}
209251
],
210252
"metadata": {

Part 2/Section 05 - Project 2/02 - Project Solution - Goal 1.ipynb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
},
2626
{
2727
"cell_type": "code",
28-
"execution_count": 9,
28+
"execution_count": 1,
2929
"metadata": {
3030
"collapsed": true
3131
},
@@ -104,7 +104,7 @@
104104
},
105105
{
106106
"cell_type": "code",
107-
"execution_count": 13,
107+
"execution_count": 2,
108108
"metadata": {
109109
"collapsed": true
110110
},
@@ -191,7 +191,7 @@
191191
},
192192
{
193193
"cell_type": "code",
194-
"execution_count": 14,
194+
"execution_count": 3,
195195
"metadata": {
196196
"collapsed": true
197197
},
@@ -281,7 +281,7 @@
281281
},
282282
{
283283
"cell_type": "code",
284-
"execution_count": 15,
284+
"execution_count": 4,
285285
"metadata": {
286286
"collapsed": true
287287
},

Part 2/Section 05 - Project 2/03 - Project Solution - Goal 2.ipynb

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@
4040
" self._n = n\n",
4141
" self._R = R\n",
4242
" \n",
43+
" self._interior_angle = None\n",
44+
" self._side_length = None\n",
45+
" self._apothem = None\n",
46+
" self._area = None\n",
47+
" self._perimeter = None\n",
48+
" \n",
4349
" def __repr__(self):\n",
4450
" return f'Polygon(n={self._n}, R={self._R})'\n",
4551
" \n",
@@ -57,23 +63,33 @@
5763
" \n",
5864
" @property\n",
5965
" def interior_angle(self):\n",
60-
" return (self._n - 2) * 180 / self._n\n",
66+
" if self._interior_angle is None:\n",
67+
" self._interior_angle = (self._n - 2) * 180 / self._n\n",
68+
" return self._interior_angle\n",
6169
"\n",
6270
" @property\n",
6371
" def side_length(self):\n",
64-
" return 2 * self._R * math.sin(math.pi / self._n)\n",
72+
" if self._side_length is None:\n",
73+
" self._side_length = 2 * self._R * math.sin(math.pi / self._n)\n",
74+
" return self._side_length\n",
6575
" \n",
6676
" @property\n",
6777
" def apothem(self):\n",
68-
" return self._R * math.cos(math.pi / self._n)\n",
78+
" if self._apothem is None:\n",
79+
" self._apothem = self._R * math.cos(math.pi / self._n)\n",
80+
" return self._apothem\n",
6981
" \n",
7082
" @property\n",
7183
" def area(self):\n",
72-
" return self._n / 2 * self.side_length * self.apothem\n",
84+
" if self._area is None:\n",
85+
" self._area = self._n / 2 * self.side_length * self.apothem\n",
86+
" return self._area\n",
7387
" \n",
7488
" @property\n",
7589
" def perimeter(self):\n",
76-
" return self._n * self.side_length\n",
90+
" if self._perimeter is None:\n",
91+
" self._perimeter = self._n * self.side_length\n",
92+
" return self._perimeter\n",
7793
" \n",
7894
" def __eq__(self, other):\n",
7995
" if isinstance(other, self.__class__):\n",
@@ -98,7 +114,7 @@
98114
},
99115
{
100116
"cell_type": "code",
101-
"execution_count": 18,
117+
"execution_count": 2,
102118
"metadata": {
103119
"collapsed": true
104120
},
@@ -138,7 +154,7 @@
138154
},
139155
{
140156
"cell_type": "code",
141-
"execution_count": 4,
157+
"execution_count": 3,
142158
"metadata": {
143159
"collapsed": true
144160
},
@@ -173,7 +189,7 @@
173189
},
174190
{
175191
"cell_type": "code",
176-
"execution_count": 23,
192+
"execution_count": 4,
177193
"metadata": {
178194
"collapsed": true
179195
},
@@ -208,7 +224,7 @@
208224
},
209225
{
210226
"cell_type": "code",
211-
"execution_count": 24,
227+
"execution_count": 5,
212228
"metadata": {},
213229
"outputs": [
214230
{
@@ -236,7 +252,7 @@
236252
},
237253
{
238254
"cell_type": "code",
239-
"execution_count": 26,
255+
"execution_count": 6,
240256
"metadata": {},
241257
"outputs": [
242258
{
@@ -245,7 +261,7 @@
245261
"[]"
246262
]
247263
},
248-
"execution_count": 26,
264+
"execution_count": 6,
249265
"metadata": {},
250266
"output_type": "execute_result"
251267
}
@@ -263,7 +279,7 @@
263279
},
264280
{
265281
"cell_type": "code",
266-
"execution_count": 19,
282+
"execution_count": 7,
267283
"metadata": {
268284
"collapsed": true
269285
},
@@ -302,7 +318,7 @@
302318
},
303319
{
304320
"cell_type": "code",
305-
"execution_count": 20,
321+
"execution_count": 8,
306322
"metadata": {
307323
"collapsed": true
308324
},
@@ -313,7 +329,7 @@
313329
},
314330
{
315331
"cell_type": "code",
316-
"execution_count": 21,
332+
"execution_count": 9,
317333
"metadata": {},
318334
"outputs": [
319335
{
@@ -333,7 +349,7 @@
333349
},
334350
{
335351
"cell_type": "code",
336-
"execution_count": 22,
352+
"execution_count": 10,
337353
"metadata": {},
338354
"outputs": [
339355
{
@@ -360,7 +376,7 @@
360376
},
361377
{
362378
"cell_type": "code",
363-
"execution_count": 32,
379+
"execution_count": 11,
364380
"metadata": {
365381
"collapsed": true
366382
},
@@ -402,7 +418,7 @@
402418
},
403419
{
404420
"cell_type": "code",
405-
"execution_count": 33,
421+
"execution_count": 12,
406422
"metadata": {},
407423
"outputs": [
408424
{
@@ -412,8 +428,8 @@
412428
"traceback": [
413429
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
414430
"\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)",
415-
"\u001b[1;32m<ipython-input-33-a69efa15bd2e>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0mpolygons\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mPolygons\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m10\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpolygons\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmax_efficiency_polygon\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
416-
"\u001b[1;32m<ipython-input-32-69e2f4e6f9be>\u001b[0m in \u001b[0;36mmax_efficiency_polygon\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m 19\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mmax_efficiency_polygon\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 20\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_max_efficiency_polygon\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 21\u001b[1;33m sorted_polygons = sorted(self._polygons, \n\u001b[0m\u001b[0;32m 22\u001b[0m \u001b[0mkey\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mlambda\u001b[0m \u001b[0mp\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0marea\u001b[0m\u001b[1;33m/\u001b[0m\u001b[0mp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mperimeter\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 23\u001b[0m reverse=True)\n",
431+
"\u001b[1;32m<ipython-input-12-a69efa15bd2e>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0mpolygons\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mPolygons\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m10\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpolygons\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmax_efficiency_polygon\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
432+
"\u001b[1;32m<ipython-input-11-69e2f4e6f9be>\u001b[0m in \u001b[0;36mmax_efficiency_polygon\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m 19\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mmax_efficiency_polygon\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 20\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_max_efficiency_polygon\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 21\u001b[1;33m sorted_polygons = sorted(self._polygons, \n\u001b[0m\u001b[0;32m 22\u001b[0m \u001b[0mkey\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mlambda\u001b[0m \u001b[0mp\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0marea\u001b[0m\u001b[1;33m/\u001b[0m\u001b[0mp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mperimeter\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 23\u001b[0m reverse=True)\n",
417433
"\u001b[1;31mAttributeError\u001b[0m: 'Polygons' object has no attribute '_polygons'"
418434
]
419435
}
@@ -434,7 +450,7 @@
434450
},
435451
{
436452
"cell_type": "code",
437-
"execution_count": 35,
453+
"execution_count": 13,
438454
"metadata": {
439455
"collapsed": true
440456
},
@@ -476,7 +492,7 @@
476492
},
477493
{
478494
"cell_type": "code",
479-
"execution_count": 36,
495+
"execution_count": 14,
480496
"metadata": {},
481497
"outputs": [
482498
{

0 commit comments

Comments
 (0)