Skip to content

Commit e35d261

Browse files
committed
update TestingFunctions-unittest
1 parent 2331267 commit e35d261

File tree

2 files changed

+146
-36
lines changed

2 files changed

+146
-36
lines changed

Diff for: demo-assignments/A2-ABC/egypt/egypt.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
accepted: 0
1+
accepted: 1
22
cpu_limit: 1 second
33
difficulty: 1.9 Easy
44
mem_limit: 1024 MB
55
problemid: egypt
6-
submissions: 0
6+
submissions: 1
77
title: Egypt

Diff for: notebooks/TestingFunctions-unittest.ipynb

+144-34
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@
152152
},
153153
{
154154
"cell_type": "code",
155-
"execution_count": 2,
155+
"execution_count": null,
156156
"id": "aa37aafc",
157157
"metadata": {},
158158
"outputs": [],
@@ -162,7 +162,7 @@
162162
},
163163
{
164164
"cell_type": "code",
165-
"execution_count": 3,
165+
"execution_count": 29,
166166
"id": "1e758601",
167167
"metadata": {},
168168
"outputs": [],
@@ -172,8 +172,22 @@
172172
" GIVEN a list, data = [1, 2, None, 3, 4]\n",
173173
" WHEN we compute m = average(data)\n",
174174
" THEN the result, m is 2.5\n",
175+
" \n",
176+
" Finds and returns average of a List of optional integers.\n",
177+
" \n",
178+
" Assumptions:\n",
179+
" - Ignore None.\n",
180+
" - Throw ZeroDivisionError if data is an empty list.\n",
175181
" \"\"\"\n",
176-
" pass\n"
182+
" tmp: float = 0\n",
183+
" len_data: int = len(data)\n",
184+
" for x in data:\n",
185+
" try:\n",
186+
" tmp += x\n",
187+
" except TypeError:\n",
188+
" len_data -= 1\n",
189+
" \n",
190+
" return tmp/len_data\n"
177191
]
178192
},
179193
{
@@ -205,7 +219,7 @@
205219
},
206220
{
207221
"cell_type": "code",
208-
"execution_count": 11,
222+
"execution_count": 22,
209223
"id": "ef522cff",
210224
"metadata": {},
211225
"outputs": [],
@@ -219,61 +233,155 @@
219233
},
220234
{
221235
"cell_type": "code",
222-
"execution_count": 12,
223-
"id": "b62bc7f7",
236+
"execution_count": null,
237+
"id": "5b38379e",
224238
"metadata": {},
225-
"outputs": [
226-
{
227-
"ename": "AssertionError",
228-
"evalue": "",
229-
"output_type": "error",
230-
"traceback": [
231-
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
232-
"\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)",
233-
"Cell \u001b[0;32mIn[12], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mtest_average1\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n",
234-
"Cell \u001b[0;32mIn[11], line 5\u001b[0m, in \u001b[0;36mtest_average1\u001b[0;34m()\u001b[0m\n\u001b[1;32m 3\u001b[0m ans \u001b[38;5;241m=\u001b[39m average([\u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m3\u001b[39m, \u001b[38;5;241m4\u001b[39m])\n\u001b[1;32m 4\u001b[0m expected \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m2.5\u001b[39m\n\u001b[0;32m----> 5\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m ans \u001b[38;5;241m==\u001b[39m expected\n",
235-
"\u001b[0;31mAssertionError\u001b[0m: "
236-
]
237-
}
238-
],
239+
"outputs": [],
239240
"source": [
240-
"test_average1()"
241+
"def test_average2():\n",
242+
" data = [1, 2, None, 3, 4]\n",
243+
" assert average(data) == 2.5"
241244
]
242245
},
243246
{
244247
"cell_type": "code",
245-
"execution_count": 8,
248+
"execution_count": null,
246249
"id": "efe65414",
247250
"metadata": {},
248251
"outputs": [],
249252
"source": [
250-
"def test_average2():\n",
251-
" assert(average([1, 1, 1, 1, None]) == 1.0)"
253+
"def test_average3():\n",
254+
" assert average([1, 1, 1, 1, None]) == 1.0"
252255
]
253256
},
254257
{
255258
"cell_type": "code",
256-
"execution_count": 9,
257-
"id": "59c6d5ac",
259+
"execution_count": 35,
260+
"id": "983d6046",
261+
"metadata": {},
262+
"outputs": [],
263+
"source": [
264+
"def test_average4():\n",
265+
" try:\n",
266+
" average([])\n",
267+
" except ZeroDivisionError:\n",
268+
" pass"
269+
]
270+
},
271+
{
272+
"cell_type": "code",
273+
"execution_count": null,
274+
"id": "3f788364",
275+
"metadata": {},
276+
"outputs": [],
277+
"source": [
278+
"def test_average5():\n",
279+
" assert average([-1, -2, None, 1, 2]) == 0"
280+
]
281+
},
282+
{
283+
"cell_type": "code",
284+
"execution_count": 36,
285+
"id": "477fdb48",
258286
"metadata": {},
259287
"outputs": [
260288
{
261-
"ename": "AssertionError",
262-
"evalue": "",
289+
"ename": "SyntaxError",
290+
"evalue": "invalid syntax (681574647.py, line 2)",
263291
"output_type": "error",
264292
"traceback": [
265-
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
266-
"\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)",
267-
"Cell \u001b[0;32mIn[9], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mtest_average2\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n",
268-
"Cell \u001b[0;32mIn[8], line 2\u001b[0m, in \u001b[0;36mtest_average2\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mtest_average2\u001b[39m():\n\u001b[0;32m----> 2\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m(average([\u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m1\u001b[39m, \u001b[38;5;28;01mNone\u001b[39;00m]) \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m1.0\u001b[39m)\n",
269-
"\u001b[0;31mAssertionError\u001b[0m: "
293+
"\u001b[0;36m Cell \u001b[0;32mIn[36], line 2\u001b[0;36m\u001b[0m\n\u001b[0;31m assert average([1, 3, 5]) == ????/\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
270294
]
271295
}
272296
],
297+
"source": [
298+
"def test_average6():\n",
299+
" # float literal comparison or code comparison\n",
300+
" # float literal comparison may require tolerance (error less than 0.0001 or 10^-4, e.g.)\n",
301+
" assert average([1, 3, 4]) == ????"
302+
]
303+
},
304+
{
305+
"cell_type": "code",
306+
"execution_count": null,
307+
"id": "ce4f8dad",
308+
"metadata": {},
309+
"outputs": [],
310+
"source": [
311+
"def test_average7():\n",
312+
" assert average([None, None, None, None]) == ????"
313+
]
314+
},
315+
{
316+
"cell_type": "code",
317+
"execution_count": 30,
318+
"id": "b62bc7f7",
319+
"metadata": {},
320+
"outputs": [],
321+
"source": [
322+
"test_average1()"
323+
]
324+
},
325+
{
326+
"cell_type": "code",
327+
"execution_count": 31,
328+
"id": "59c6d5ac",
329+
"metadata": {},
330+
"outputs": [],
273331
"source": [
274332
"test_average2()"
275333
]
276334
},
335+
{
336+
"cell_type": "code",
337+
"execution_count": 32,
338+
"id": "466f4670",
339+
"metadata": {},
340+
"outputs": [],
341+
"source": [
342+
"test_average3()"
343+
]
344+
},
345+
{
346+
"cell_type": "code",
347+
"execution_count": null,
348+
"id": "fd5bade7",
349+
"metadata": {},
350+
"outputs": [],
351+
"source": [
352+
"test_average4()"
353+
]
354+
},
355+
{
356+
"cell_type": "code",
357+
"execution_count": null,
358+
"id": "c5044622",
359+
"metadata": {},
360+
"outputs": [],
361+
"source": [
362+
"test_average5()"
363+
]
364+
},
365+
{
366+
"cell_type": "code",
367+
"execution_count": null,
368+
"id": "3151739e",
369+
"metadata": {},
370+
"outputs": [],
371+
"source": [
372+
"test_average6()"
373+
]
374+
},
375+
{
376+
"cell_type": "code",
377+
"execution_count": null,
378+
"id": "33dad625",
379+
"metadata": {},
380+
"outputs": [],
381+
"source": [
382+
"test_average7()"
383+
]
384+
},
277385
{
278386
"cell_type": "markdown",
279387
"id": "26a6e00c",
@@ -302,7 +410,7 @@
302410
},
303411
{
304412
"cell_type": "code",
305-
"execution_count": 13,
413+
"execution_count": 3,
306414
"id": "1774d98a-83ac-4ebb-94a6-07e2d2aa9f60",
307415
"metadata": {},
308416
"outputs": [],
@@ -314,6 +422,8 @@
314422
" ans = average([1, 2, 3, 4])\n",
315423
" expected = 2.5\n",
316424
" assert ans == expected\n",
425+
" # better\n",
426+
" self.assertEqual(ans, expected, f'{ans} != {expected}')\n",
317427
"\n",
318428
" # implement test_average2"
319429
]

0 commit comments

Comments
 (0)