Skip to content

Commit

Permalink
Fix various issues in datatypes notebook (#230)
Browse files Browse the repository at this point in the history
  • Loading branch information
yakutovicha authored May 3, 2024
1 parent dfe2bba commit 8b97aac
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 40 deletions.
42 changes: 21 additions & 21 deletions basic_datatypes.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,8 @@
"%%ipytest\n",
"def solution_addition_multiplication(a: float, b:float, c:float) -> float:\n",
" # Your code starts here\n",
" solution =\n",
" # Your code ends here\n",
" return solution\n"
" return\n",
" # Your code ends here"
]
},
{
Expand All @@ -363,10 +362,8 @@
"%%ipytest\n",
"def solution_circle_area(r: float) -> float:\n",
" # Your code starts here\n",
" solution =\n",
" # Your code ends here\n",
" return solution\n",
" "
" return\n",
" # Your code ends here"
]
},
{
Expand All @@ -378,10 +375,11 @@
"outputs": [],
"source": [
"%%ipytest\n",
"\n",
"def solution_quadratic_equation(a: float, b: float, c: float) -> float:\n",
" # Your code starts here\n",
" solution1 =\n",
" solution2 =\n",
" solution1 = 0.0\n",
" solution2 = 0.0\n",
" # Your code ends here\n",
" return solution1, solution2"
]
Expand Down Expand Up @@ -1338,7 +1336,7 @@
"source": [
"%%ipytest\n",
"\n",
"def solution_lists_are_equal_but_not_same(list1: list, list2: list) -> bool:\n",
"def solution_lists_are_not_same_but_equal(list1: list, list2: list) -> bool:\n",
" # Your code starts here\n",
" return\n",
" # Your code ends here"
Expand Down Expand Up @@ -2062,9 +2060,9 @@
"source": [
"## Exercises on dictionaries\n",
"\n",
"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",
"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",
"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."
"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",
"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",
"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."
]
},
{
Expand All @@ -2087,9 +2085,9 @@
"outputs": [],
"source": [
"%%ipytest\n",
"import typing\n",
"T = typing.Typevar(\"T\")\n",
"def solution_dict_return_value(my_dict: dict[typing.Hashable, T], key: typing.Hashable) -> typing.Optional[T]:\n",
"from typing import TypeVar, Hashable, Any\n",
"\n",
"def solution_dict_return_value(my_dict: dict[Hashable, Any], key: Hashable) -> Any:\n",
" # Your code starts here\n",
" return\n",
" # Your code ends here"
Expand All @@ -2104,9 +2102,9 @@
"outputs": [],
"source": [
"%%ipytest\n",
"import typing\n",
"T = typing.Typevar(\"T\")\n",
"def solution_dict_return_value_delete(my_dict: dict[typing.Hashable, T], key: typing.Hashable) -> typing.Optional[T]:\n",
"from typing import TypeVar, Hashable, Any\n",
"\n",
"def solution_dict_return_delete_value(my_dict: dict[Hashable, Any], key: Hashable) -> Any:\n",
" # Your code starts here\n",
" return\n",
" # Your code ends here"
Expand All @@ -2122,9 +2120,11 @@
"source": [
"%%ipytest\n",
"\n",
"def solution_update_one_dict_with_another(dict1: dict, dict2: dict) -> dict:\n",
"from typing import TypeVar, Hashable, Any\n",
"\n",
"def solution_update_one_dict_with_another(dict1: dict[Hashable, Any], dict2: dict[Hashable, Any]) -> dict[Hashable, Any]:\n",
" # Your code starts here\n",
" pass\n",
" return\n",
" # Your code ends here"
]
},
Expand Down
65 changes: 46 additions & 19 deletions tutorial/tests/test_basic_datatypes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import copy
import math
from typing import Callable, Hashable, Iterable, TypeVar
from typing import Any, Callable, Hashable, Iterable

import pytest

Expand Down Expand Up @@ -234,13 +234,13 @@ def test_lists_are_equal(list1, list2, function_to_test):
assert function_to_test(list1, list2) == reference_lists_are_equal(list1, list2)


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


@pytest.mark.parametrize("list1, list2", LIST1_2)
def test_lists_are_equal_but_not_same(list1, list2, function_to_test):
assert function_to_test(list1, list2) == reference_lists_are_equal_but_not_same(
def test_lists_are_not_same_but_equal(list1, list2, function_to_test):
assert function_to_test(list1, list2) == reference_lists_are_not_same_but_equal(
list1, list2
)

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


def reference_sets_union(set1: set, set2: set) -> set:
return set1.union(set2)


@pytest.mark.parametrize("set1, set2", SET1_2)
def test_sets_union(set1, set2, function_to_test):
"""The test case(s)"""
assert function_to_test(set1, set2) == set1.union(set2)
assert function_to_test(set1, set2) == reference_sets_union(set1, set2)


def reference_sets_intersection(set1: set, set2: set) -> set:
return set1.intersection(set2)


@pytest.mark.parametrize("set1, set2", SET1_2)
def test_sets_intersection(set1, set2, function_to_test):
"""The test case(s)"""
assert function_to_test(set1, set2) == set1.intersection(set2)
assert function_to_test(set1, set2) == reference_sets_intersection(set1, set2)


def reference_sets_difference(set1: set, set2: set) -> set:
return set1.difference(set2)


@pytest.mark.parametrize("set1, set2", SET1_2)
def test_sets_difference(set1, set2, function_to_test):
"""The test case(s)"""
assert function_to_test(set1, set2) == set1.difference(set2)
assert function_to_test(set1, set2) == reference_sets_difference(set1, set2)


def reference_sets_symmetric_difference(set1: set, set2: set) -> set:
return set1.symmetric_difference(set2)


@pytest.mark.parametrize("set1, set2", SET1_2)
def test_sets_symmetric_difference(set1, set2, function_to_test):
"""The test case(s)"""
assert function_to_test(set1, set2) == set1.symmetric_difference(set2)
assert function_to_test(set1, set2) == reference_sets_symmetric_difference(
set1, set2
)


def reference_sets_subset(set1: set, set2: set) -> bool:
return set1.issubset(set2)


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


def reference_sets_superset(set1: set, set2: set) -> bool:
return set1.issuperset(set2)


@pytest.mark.parametrize("set1, set2", SET1_2)
def test_sets_superset(set1, set2, function_to_test):
"""The test case(s)"""
assert function_to_test(set1, set2) == set1.issuperset(set2)
assert function_to_test(set1, set2) == reference_sets_superset(set1, set2)


def reference_sets_disjoint(set1: set, set2: set) -> bool:
return set1.isdisjoint(set2)


@pytest.mark.parametrize("set1, set2", SET1_2)
def test_sets_disjoint(set1, set2, function_to_test):
"""The test case(s)"""
assert function_to_test(set1, set2) == set1.isdisjoint(set2)
assert function_to_test(set1, set2) == reference_sets_disjoint(set1, set2)


DICTS1 = [
Expand All @@ -326,11 +356,8 @@ def test_sets_disjoint(set1, set2, function_to_test):
{},
]

V = TypeVar("V")
K = TypeVar("K", bound=Hashable)


def reference_dict_return_value(my_dict: dict[K, V], key: K) -> V | None:
def reference_dict_return_value(my_dict: dict[Hashable, Any], key: Any) -> Any:
return my_dict.get(key)


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


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


@pytest.mark.parametrize(
"my_dict, key", list(zip(copy.deepcopy(DICTS1), ["b"] * len(DICTS1)))
)
def test_dict_return_value_delete(my_dict, key, function_to_test):
def test_dict_return_delete_value(my_dict, key, function_to_test):
my_dict_original1 = my_dict.copy()
my_dict_original2 = my_dict.copy()
assert function_to_test(
my_dict_original1, key
) == reference_dict_return_value_delete(my_dict_original2, key)
) == reference_dict_return_delete_value(my_dict_original2, key)

assert my_dict_original1 == my_dict_original2


def reference_update_one_dict_with_another(
dict1: dict[K, V], dict2: dict[K, V]
) -> None:
dict1: dict[Hashable, Any], dict2: dict[Hashable, Any]
) -> dict[Hashable, Any]:
return dict1.update(dict2)


Expand Down

0 comments on commit 8b97aac

Please sign in to comment.