Skip to content

Commit 8b97aac

Browse files
authored
Fix various issues in datatypes notebook (#230)
1 parent dfe2bba commit 8b97aac

2 files changed

Lines changed: 67 additions & 40 deletions

File tree

basic_datatypes.ipynb

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -347,9 +347,8 @@
347347
"%%ipytest\n",
348348
"def solution_addition_multiplication(a: float, b:float, c:float) -> float:\n",
349349
" # Your code starts here\n",
350-
" solution =\n",
351-
" # Your code ends here\n",
352-
" return solution\n"
350+
" return\n",
351+
" # Your code ends here"
353352
]
354353
},
355354
{
@@ -363,10 +362,8 @@
363362
"%%ipytest\n",
364363
"def solution_circle_area(r: float) -> float:\n",
365364
" # Your code starts here\n",
366-
" solution =\n",
367-
" # Your code ends here\n",
368-
" return solution\n",
369-
" "
365+
" return\n",
366+
" # Your code ends here"
370367
]
371368
},
372369
{
@@ -378,10 +375,11 @@
378375
"outputs": [],
379376
"source": [
380377
"%%ipytest\n",
378+
"\n",
381379
"def solution_quadratic_equation(a: float, b: float, c: float) -> float:\n",
382380
" # Your code starts here\n",
383-
" solution1 =\n",
384-
" solution2 =\n",
381+
" solution1 = 0.0\n",
382+
" solution2 = 0.0\n",
385383
" # Your code ends here\n",
386384
" return solution1, solution2"
387385
]
@@ -1338,7 +1336,7 @@
13381336
"source": [
13391337
"%%ipytest\n",
13401338
"\n",
1341-
"def solution_lists_are_equal_but_not_same(list1: list, list2: list) -> bool:\n",
1339+
"def solution_lists_are_not_same_but_equal(list1: list, list2: list) -> bool:\n",
13421340
" # Your code starts here\n",
13431341
" return\n",
13441342
" # Your code ends here"
@@ -2062,9 +2060,9 @@
20622060
"source": [
20632061
"## Exercises on dictionaries\n",
20642062
"\n",
2065-
"1. Write a program that recieves a dictionary and a key as input and returns the value of the key if it is in the dictionary, `None` otherwise. The dictionary should remain unchanged.\n",
2066-
"2. Write a program that recieves a dictionary and a key as input and removes the key-value pair from the dictionary and returns the value. If the key is not in the dictionary, the program should return `None`.\n",
2067-
"3. Write a program that recieves two dictionaries as input and returns the first dictionary updated with the key-value pairs from the second dictionary. The second dictionary should remain unchanged."
2063+
"1. Write a program that receives a dictionary and a key as input and returns the value of the key if it is in the dictionary, `None` otherwise. The dictionary should remain unchanged.\n",
2064+
"2. Write a program that receives a dictionary and a key as input and removes the key-value pair from the dictionary and returns the value. If the key is not in the dictionary, the program should return `None`.\n",
2065+
"3. Write a program that receives two dictionaries as input and returns the first dictionary updated with the key-value pairs from the second dictionary. The second dictionary should remain unchanged."
20682066
]
20692067
},
20702068
{
@@ -2087,9 +2085,9 @@
20872085
"outputs": [],
20882086
"source": [
20892087
"%%ipytest\n",
2090-
"import typing\n",
2091-
"T = typing.Typevar(\"T\")\n",
2092-
"def solution_dict_return_value(my_dict: dict[typing.Hashable, T], key: typing.Hashable) -> typing.Optional[T]:\n",
2088+
"from typing import TypeVar, Hashable, Any\n",
2089+
"\n",
2090+
"def solution_dict_return_value(my_dict: dict[Hashable, Any], key: Hashable) -> Any:\n",
20932091
" # Your code starts here\n",
20942092
" return\n",
20952093
" # Your code ends here"
@@ -2104,9 +2102,9 @@
21042102
"outputs": [],
21052103
"source": [
21062104
"%%ipytest\n",
2107-
"import typing\n",
2108-
"T = typing.Typevar(\"T\")\n",
2109-
"def solution_dict_return_value_delete(my_dict: dict[typing.Hashable, T], key: typing.Hashable) -> typing.Optional[T]:\n",
2105+
"from typing import TypeVar, Hashable, Any\n",
2106+
"\n",
2107+
"def solution_dict_return_delete_value(my_dict: dict[Hashable, Any], key: Hashable) -> Any:\n",
21102108
" # Your code starts here\n",
21112109
" return\n",
21122110
" # Your code ends here"
@@ -2122,9 +2120,11 @@
21222120
"source": [
21232121
"%%ipytest\n",
21242122
"\n",
2125-
"def solution_update_one_dict_with_another(dict1: dict, dict2: dict) -> dict:\n",
2123+
"from typing import TypeVar, Hashable, Any\n",
2124+
"\n",
2125+
"def solution_update_one_dict_with_another(dict1: dict[Hashable, Any], dict2: dict[Hashable, Any]) -> dict[Hashable, Any]:\n",
21262126
" # Your code starts here\n",
2127-
" pass\n",
2127+
" return\n",
21282128
" # Your code ends here"
21292129
]
21302130
},

tutorial/tests/test_basic_datatypes.py

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import copy
22
import math
3-
from typing import Callable, Hashable, Iterable, TypeVar
3+
from typing import Any, Callable, Hashable, Iterable
44

55
import pytest
66

@@ -234,13 +234,13 @@ def test_lists_are_equal(list1, list2, function_to_test):
234234
assert function_to_test(list1, list2) == reference_lists_are_equal(list1, list2)
235235

236236

237-
def reference_lists_are_equal_but_not_same(list1: list, list2: list) -> bool:
237+
def reference_lists_are_not_same_but_equal(list1: list, list2: list) -> bool:
238238
return list1 == list2 and list1 is not list2
239239

240240

241241
@pytest.mark.parametrize("list1, list2", LIST1_2)
242-
def test_lists_are_equal_but_not_same(list1, list2, function_to_test):
243-
assert function_to_test(list1, list2) == reference_lists_are_equal_but_not_same(
242+
def test_lists_are_not_same_but_equal(list1, list2, function_to_test):
243+
assert function_to_test(list1, list2) == reference_lists_are_not_same_but_equal(
244244
list1, list2
245245
)
246246

@@ -266,28 +266,50 @@ def test_greater_or_equal(list1, list2, function_to_test):
266266
]
267267

268268

269+
def reference_sets_union(set1: set, set2: set) -> set:
270+
return set1.union(set2)
271+
272+
269273
@pytest.mark.parametrize("set1, set2", SET1_2)
270274
def test_sets_union(set1, set2, function_to_test):
271275
"""The test case(s)"""
272-
assert function_to_test(set1, set2) == set1.union(set2)
276+
assert function_to_test(set1, set2) == reference_sets_union(set1, set2)
277+
278+
279+
def reference_sets_intersection(set1: set, set2: set) -> set:
280+
return set1.intersection(set2)
273281

274282

275283
@pytest.mark.parametrize("set1, set2", SET1_2)
276284
def test_sets_intersection(set1, set2, function_to_test):
277285
"""The test case(s)"""
278-
assert function_to_test(set1, set2) == set1.intersection(set2)
286+
assert function_to_test(set1, set2) == reference_sets_intersection(set1, set2)
287+
288+
289+
def reference_sets_difference(set1: set, set2: set) -> set:
290+
return set1.difference(set2)
279291

280292

281293
@pytest.mark.parametrize("set1, set2", SET1_2)
282294
def test_sets_difference(set1, set2, function_to_test):
283295
"""The test case(s)"""
284-
assert function_to_test(set1, set2) == set1.difference(set2)
296+
assert function_to_test(set1, set2) == reference_sets_difference(set1, set2)
297+
298+
299+
def reference_sets_symmetric_difference(set1: set, set2: set) -> set:
300+
return set1.symmetric_difference(set2)
285301

286302

287303
@pytest.mark.parametrize("set1, set2", SET1_2)
288304
def test_sets_symmetric_difference(set1, set2, function_to_test):
289305
"""The test case(s)"""
290-
assert function_to_test(set1, set2) == set1.symmetric_difference(set2)
306+
assert function_to_test(set1, set2) == reference_sets_symmetric_difference(
307+
set1, set2
308+
)
309+
310+
311+
def reference_sets_subset(set1: set, set2: set) -> bool:
312+
return set1.issubset(set2)
291313

292314

293315
@pytest.mark.parametrize("set1, set2", SET1_2)
@@ -296,16 +318,24 @@ def test_sets_subset(set1, set2, function_to_test):
296318
assert function_to_test(set1, set2) == set1.issubset(set2)
297319

298320

321+
def reference_sets_superset(set1: set, set2: set) -> bool:
322+
return set1.issuperset(set2)
323+
324+
299325
@pytest.mark.parametrize("set1, set2", SET1_2)
300326
def test_sets_superset(set1, set2, function_to_test):
301327
"""The test case(s)"""
302-
assert function_to_test(set1, set2) == set1.issuperset(set2)
328+
assert function_to_test(set1, set2) == reference_sets_superset(set1, set2)
329+
330+
331+
def reference_sets_disjoint(set1: set, set2: set) -> bool:
332+
return set1.isdisjoint(set2)
303333

304334

305335
@pytest.mark.parametrize("set1, set2", SET1_2)
306336
def test_sets_disjoint(set1, set2, function_to_test):
307337
"""The test case(s)"""
308-
assert function_to_test(set1, set2) == set1.isdisjoint(set2)
338+
assert function_to_test(set1, set2) == reference_sets_disjoint(set1, set2)
309339

310340

311341
DICTS1 = [
@@ -326,11 +356,8 @@ def test_sets_disjoint(set1, set2, function_to_test):
326356
{},
327357
]
328358

329-
V = TypeVar("V")
330-
K = TypeVar("K", bound=Hashable)
331359

332-
333-
def reference_dict_return_value(my_dict: dict[K, V], key: K) -> V | None:
360+
def reference_dict_return_value(my_dict: dict[Hashable, Any], key: Any) -> Any:
334361
return my_dict.get(key)
335362

336363

@@ -346,26 +373,26 @@ def test_dict_return_value(my_dict, key, function_to_test):
346373
assert my_dict == my_dict_to_try
347374

348375

349-
def reference_dict_return_value_delete(my_dict: dict[K, V], key: K) -> V | None:
376+
def reference_dict_return_delete_value(my_dict: dict[Hashable, Any], key: Any) -> Any:
350377
return my_dict.pop(key, None)
351378

352379

353380
@pytest.mark.parametrize(
354381
"my_dict, key", list(zip(copy.deepcopy(DICTS1), ["b"] * len(DICTS1)))
355382
)
356-
def test_dict_return_value_delete(my_dict, key, function_to_test):
383+
def test_dict_return_delete_value(my_dict, key, function_to_test):
357384
my_dict_original1 = my_dict.copy()
358385
my_dict_original2 = my_dict.copy()
359386
assert function_to_test(
360387
my_dict_original1, key
361-
) == reference_dict_return_value_delete(my_dict_original2, key)
388+
) == reference_dict_return_delete_value(my_dict_original2, key)
362389

363390
assert my_dict_original1 == my_dict_original2
364391

365392

366393
def reference_update_one_dict_with_another(
367-
dict1: dict[K, V], dict2: dict[K, V]
368-
) -> None:
394+
dict1: dict[Hashable, Any], dict2: dict[Hashable, Any]
395+
) -> dict[Hashable, Any]:
369396
return dict1.update(dict2)
370397

371398

0 commit comments

Comments
 (0)