Skip to content

Commit

Permalink
Added type hints. (#219)
Browse files Browse the repository at this point in the history
* Added type hints.

* Added type hints to solutions

* fix pre-commit

---------

Co-authored-by: Aliaksandr Yakutovich <yakutovicha@gmail.com>
  • Loading branch information
baffelli and yakutovicha authored Apr 30, 2024
1 parent 08a2244 commit 89d465e
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 63 deletions.
64 changes: 33 additions & 31 deletions basic_datatypes.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@
"outputs": [],
"source": [
"%%ipytest\n",
"def solution_addition_multiplication(a, b, c):\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",
Expand All @@ -361,7 +361,7 @@
"outputs": [],
"source": [
"%%ipytest\n",
"def solution_circle_area(r):\n",
"def solution_circle_area(r: float) -> float:\n",
" # Your code starts here\n",
" solution =\n",
" # Your code ends here\n",
Expand All @@ -378,7 +378,7 @@
"outputs": [],
"source": [
"%%ipytest\n",
"def solution_quadratic_equation(a, b, c):\n",
"def solution_quadratic_equation(a: float, b: float, c: float) -> float:\n",
" # Your code starts here\n",
" solution1 =\n",
" solution2 =\n",
Expand Down Expand Up @@ -689,7 +689,7 @@
"source": [
"%%ipytest\n",
"\n",
"def solution_a_plus_b_equals_c(a, b, c):\n",
"def solution_a_plus_b_equals_c(a: float, b: float, c: float) -> float:\n",
" # Your code starts here\n",
" return\n",
" # Your code ends here\n"
Expand All @@ -705,7 +705,7 @@
"source": [
"%%ipytest\n",
"\n",
"def solution_number_is_even(number):\n",
"def solution_number_is_even(number: float) -> bool:\n",
" # Your code starts here\n",
" return number\n",
" # Your code ends here\n"
Expand All @@ -719,7 +719,7 @@
"source": [
"%%ipytest\n",
"\n",
"def solution_number_is_greater_than_zero(number):\n",
"def solution_number_is_greater_than_zero(number: float) -> bool:\n",
" # Your code starts here\n",
" return\n",
" # Your code ends here"
Expand Down Expand Up @@ -910,7 +910,7 @@
"source": [
"%%ipytest\n",
"\n",
"def solution_number_is_positive_and_even(number):\n",
"def solution_number_is_positive_and_even(number: float) -> bool:\n",
" # Your code starts here\n",
" return\n",
" # Your code ends here"
Expand All @@ -926,7 +926,7 @@
"source": [
"%%ipytest\n",
"\n",
"def solution_number_is_lower_than_0_or_greater_than_100(number):\n",
"def solution_number_is_lower_than_0_or_greater_than_100(number: float) -> bool:\n",
" # Your code starts here\n",
" return\n",
" # Your code ends here"
Expand Down Expand Up @@ -1272,7 +1272,7 @@
"source": [
"%%ipytest\n",
"\n",
"def solution_remove_every_second_element_from_list(my_list):\n",
"def solution_remove_every_second_element_from_list(my_list: list[float]) -> list[float]:\n",
" # Your code starts here\n",
" return\n",
" # Your code ends here"
Expand All @@ -1288,7 +1288,7 @@
"source": [
"%%ipytest\n",
"\n",
"def solution_return_first_and_last_element_from_list(my_list):\n",
"def solution_return_first_and_last_element_from_list(my_list: list[float]) -> list[float]:\n",
" # Your code starts here\n",
" return\n",
" # Your code ends here"
Expand All @@ -1304,7 +1304,7 @@
"source": [
"%%ipytest\n",
"\n",
"def solution_first_and_last_element_are_equal(my_list):\n",
"def solution_first_and_last_element_are_equal(my_list: list[float]) -> bool:\n",
" # Your code starts here\n",
" return\n",
" # Your code ends here"
Expand All @@ -1320,7 +1320,7 @@
"source": [
"%%ipytest\n",
"\n",
"def solution_lists_are_equal(list1, list2):\n",
"def solution_lists_are_equal(list1: list, list2: list) -> bool:\n",
" # Your code starts here\n",
" return\n",
" # Your code ends here"
Expand All @@ -1336,7 +1336,7 @@
"source": [
"%%ipytest\n",
"\n",
"def solution_lists_are_equal_but_not_same(list1, list2):\n",
"def solution_lists_are_equal_but_not_same(list1: list, list2: list) -> bool:\n",
" # Your code starts here\n",
" return\n",
" # Your code ends here"
Expand All @@ -1352,7 +1352,7 @@
"source": [
"%%ipytest\n",
"\n",
"def solution_greater_or_equal(list1, list2):\n",
"def solution_greater_or_equal(list1: list, list2: list) -> bool:\n",
" # Your code starts here\n",
" return\n",
" # Your code ends here"
Expand Down Expand Up @@ -1832,7 +1832,7 @@
"source": [
"%%ipytest\n",
"\n",
"def solution_sets_union(set1, set2):\n",
"def solution_sets_union(set1: set, set2: set) -> set:\n",
" # Your code starts here\n",
" return\n",
" # Your code ends here"
Expand All @@ -1846,7 +1846,7 @@
"source": [
"%%ipytest\n",
"\n",
"def solution_sets_intersection(set1, set2):\n",
"def solution_sets_intersection(set1: set, set2: set) -> set:\n",
" # Your code starts here\n",
" return\n",
" # Your code ends here"
Expand All @@ -1860,7 +1860,7 @@
"source": [
"%%ipytest\n",
"\n",
"def solution_sets_difference(set1, set2):\n",
"def solution_sets_difference(set1: set, set2: set) -> set:\n",
" # Your code starts here\n",
" return\n",
" # Your code ends here"
Expand All @@ -1874,7 +1874,7 @@
"source": [
"%%ipytest\n",
"\n",
"def solution_sets_symmetric_difference(set1, set2):\n",
"def solution_sets_symmetric_difference(set1: set, set2: set) -> set:\n",
" # Your code starts here\n",
" return\n",
" # Your code ends here "
Expand All @@ -1888,7 +1888,7 @@
"source": [
"%%ipytest\n",
"\n",
"def solution_sets_subset(set1, set2):\n",
"def solution_sets_subset(set1: set, set2: set) -> set:\n",
" # Your code starts here\n",
" return\n",
" # Your code ends here"
Expand All @@ -1902,7 +1902,7 @@
"source": [
"%%ipytest\n",
"\n",
"def solution_sets_superset(set1, set2):\n",
"def solution_sets_superset(set1: set, set2: set) -> set:\n",
" # Your code starts here\n",
" return\n",
" # Your code ends here"
Expand All @@ -1918,7 +1918,7 @@
"source": [
"%%ipytest\n",
"\n",
"def solution_sets_disjoint(set1, set2):\n",
"def solution_sets_disjoint(set1: set, set2: set) -> set:\n",
" # Your code starts here\n",
" return\n",
" # Your code ends here"
Expand Down Expand Up @@ -2085,8 +2085,9 @@
"outputs": [],
"source": [
"%%ipytest\n",
"\n",
"def solution_dict_return_value(my_dict, key):\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",
" # Your code starts here\n",
" return\n",
" # Your code ends here"
Expand All @@ -2101,8 +2102,9 @@
"outputs": [],
"source": [
"%%ipytest\n",
"\n",
"def solution_dict_return_value_delete(my_dict, key):\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",
" # Your code starts here\n",
" return\n",
" # Your code ends here"
Expand All @@ -2118,7 +2120,7 @@
"source": [
"%%ipytest\n",
"\n",
"def solution_update_one_dict_with_another(dict1, dict2):\n",
"def solution_update_one_dict_with_another(dict1: dict, dict2: dict) -> dict:\n",
" # Your code starts here\n",
" pass\n",
" # Your code ends here"
Expand Down Expand Up @@ -2263,7 +2265,7 @@
"source": [
"%%ipytest\n",
"\n",
"def solution_string_capitalize(my_string):\n",
"def solution_string_capitalize(my_string: str) -> str:\n",
" # Your code starts here\n",
" return\n",
" # Your code ends here"
Expand All @@ -2279,7 +2281,7 @@
"source": [
"%%ipytest\n",
"\n",
"def solution_string_lower_case(my_string):\n",
"def solution_string_lower_case(my_string: str) -> str:\n",
" # Your code starts here\n",
" return\n",
" # Your code ends here"
Expand All @@ -2295,7 +2297,7 @@
"source": [
"%%ipytest\n",
"\n",
"def solution_string_word_split(my_string):\n",
"def solution_string_word_split(my_string: str) -> str:\n",
" # Your code starts here\n",
" return\n",
" # Your code ends here"
Expand All @@ -2311,7 +2313,7 @@
"source": [
"%%ipytest\n",
"\n",
"def solution_string_join_commas(my_list):\n",
"def solution_string_join_commas(my_list: str) -> str:\n",
" # Your code starts here\n",
" return\n",
" # Your code ends here"
Expand All @@ -2327,7 +2329,7 @@
"source": [
"%%ipytest\n",
"\n",
"def solution_string_split_lines(my_string):\n",
"def solution_string_split_lines(my_string: str) -> list[str]:\n",
" # Your code starts here\n",
" return\n",
" # Your code ends here"
Expand Down
2 changes: 1 addition & 1 deletion tutorial/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def __init__(self, questions: list = None) -> None:
def add_question(self, question: Question):
"""Adds a question to the quiz."""
question.question.value = (
f"""<strong>Q{self.nquestions+1}:</strong> """ + question.question.value
f"""<strong>Q{self.nquestions + 1}:</strong> """ + question.question.value
)
self.questions.append(question)
self.children = self.questions + self.aux
Expand Down
Loading

0 comments on commit 89d465e

Please sign in to comment.