Skip to content

Commit 89d465e

Browse files
Added type hints. (#219)
* Added type hints. * Added type hints to solutions * fix pre-commit --------- Co-authored-by: Aliaksandr Yakutovich <yakutovicha@gmail.com>
1 parent 08a2244 commit 89d465e

4 files changed

Lines changed: 92 additions & 63 deletions

File tree

basic_datatypes.ipynb

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@
345345
"outputs": [],
346346
"source": [
347347
"%%ipytest\n",
348-
"def solution_addition_multiplication(a, b, c):\n",
348+
"def solution_addition_multiplication(a: float, b:float, c:float) -> float:\n",
349349
" # Your code starts here\n",
350350
" solution =\n",
351351
" # Your code ends here\n",
@@ -361,7 +361,7 @@
361361
"outputs": [],
362362
"source": [
363363
"%%ipytest\n",
364-
"def solution_circle_area(r):\n",
364+
"def solution_circle_area(r: float) -> float:\n",
365365
" # Your code starts here\n",
366366
" solution =\n",
367367
" # Your code ends here\n",
@@ -378,7 +378,7 @@
378378
"outputs": [],
379379
"source": [
380380
"%%ipytest\n",
381-
"def solution_quadratic_equation(a, b, c):\n",
381+
"def solution_quadratic_equation(a: float, b: float, c: float) -> float:\n",
382382
" # Your code starts here\n",
383383
" solution1 =\n",
384384
" solution2 =\n",
@@ -689,7 +689,7 @@
689689
"source": [
690690
"%%ipytest\n",
691691
"\n",
692-
"def solution_a_plus_b_equals_c(a, b, c):\n",
692+
"def solution_a_plus_b_equals_c(a: float, b: float, c: float) -> float:\n",
693693
" # Your code starts here\n",
694694
" return\n",
695695
" # Your code ends here\n"
@@ -705,7 +705,7 @@
705705
"source": [
706706
"%%ipytest\n",
707707
"\n",
708-
"def solution_number_is_even(number):\n",
708+
"def solution_number_is_even(number: float) -> bool:\n",
709709
" # Your code starts here\n",
710710
" return number\n",
711711
" # Your code ends here\n"
@@ -719,7 +719,7 @@
719719
"source": [
720720
"%%ipytest\n",
721721
"\n",
722-
"def solution_number_is_greater_than_zero(number):\n",
722+
"def solution_number_is_greater_than_zero(number: float) -> bool:\n",
723723
" # Your code starts here\n",
724724
" return\n",
725725
" # Your code ends here"
@@ -910,7 +910,7 @@
910910
"source": [
911911
"%%ipytest\n",
912912
"\n",
913-
"def solution_number_is_positive_and_even(number):\n",
913+
"def solution_number_is_positive_and_even(number: float) -> bool:\n",
914914
" # Your code starts here\n",
915915
" return\n",
916916
" # Your code ends here"
@@ -926,7 +926,7 @@
926926
"source": [
927927
"%%ipytest\n",
928928
"\n",
929-
"def solution_number_is_lower_than_0_or_greater_than_100(number):\n",
929+
"def solution_number_is_lower_than_0_or_greater_than_100(number: float) -> bool:\n",
930930
" # Your code starts here\n",
931931
" return\n",
932932
" # Your code ends here"
@@ -1272,7 +1272,7 @@
12721272
"source": [
12731273
"%%ipytest\n",
12741274
"\n",
1275-
"def solution_remove_every_second_element_from_list(my_list):\n",
1275+
"def solution_remove_every_second_element_from_list(my_list: list[float]) -> list[float]:\n",
12761276
" # Your code starts here\n",
12771277
" return\n",
12781278
" # Your code ends here"
@@ -1288,7 +1288,7 @@
12881288
"source": [
12891289
"%%ipytest\n",
12901290
"\n",
1291-
"def solution_return_first_and_last_element_from_list(my_list):\n",
1291+
"def solution_return_first_and_last_element_from_list(my_list: list[float]) -> list[float]:\n",
12921292
" # Your code starts here\n",
12931293
" return\n",
12941294
" # Your code ends here"
@@ -1304,7 +1304,7 @@
13041304
"source": [
13051305
"%%ipytest\n",
13061306
"\n",
1307-
"def solution_first_and_last_element_are_equal(my_list):\n",
1307+
"def solution_first_and_last_element_are_equal(my_list: list[float]) -> bool:\n",
13081308
" # Your code starts here\n",
13091309
" return\n",
13101310
" # Your code ends here"
@@ -1320,7 +1320,7 @@
13201320
"source": [
13211321
"%%ipytest\n",
13221322
"\n",
1323-
"def solution_lists_are_equal(list1, list2):\n",
1323+
"def solution_lists_are_equal(list1: list, list2: list) -> bool:\n",
13241324
" # Your code starts here\n",
13251325
" return\n",
13261326
" # Your code ends here"
@@ -1336,7 +1336,7 @@
13361336
"source": [
13371337
"%%ipytest\n",
13381338
"\n",
1339-
"def solution_lists_are_equal_but_not_same(list1, list2):\n",
1339+
"def solution_lists_are_equal_but_not_same(list1: list, list2: list) -> bool:\n",
13401340
" # Your code starts here\n",
13411341
" return\n",
13421342
" # Your code ends here"
@@ -1352,7 +1352,7 @@
13521352
"source": [
13531353
"%%ipytest\n",
13541354
"\n",
1355-
"def solution_greater_or_equal(list1, list2):\n",
1355+
"def solution_greater_or_equal(list1: list, list2: list) -> bool:\n",
13561356
" # Your code starts here\n",
13571357
" return\n",
13581358
" # Your code ends here"
@@ -1832,7 +1832,7 @@
18321832
"source": [
18331833
"%%ipytest\n",
18341834
"\n",
1835-
"def solution_sets_union(set1, set2):\n",
1835+
"def solution_sets_union(set1: set, set2: set) -> set:\n",
18361836
" # Your code starts here\n",
18371837
" return\n",
18381838
" # Your code ends here"
@@ -1846,7 +1846,7 @@
18461846
"source": [
18471847
"%%ipytest\n",
18481848
"\n",
1849-
"def solution_sets_intersection(set1, set2):\n",
1849+
"def solution_sets_intersection(set1: set, set2: set) -> set:\n",
18501850
" # Your code starts here\n",
18511851
" return\n",
18521852
" # Your code ends here"
@@ -1860,7 +1860,7 @@
18601860
"source": [
18611861
"%%ipytest\n",
18621862
"\n",
1863-
"def solution_sets_difference(set1, set2):\n",
1863+
"def solution_sets_difference(set1: set, set2: set) -> set:\n",
18641864
" # Your code starts here\n",
18651865
" return\n",
18661866
" # Your code ends here"
@@ -1874,7 +1874,7 @@
18741874
"source": [
18751875
"%%ipytest\n",
18761876
"\n",
1877-
"def solution_sets_symmetric_difference(set1, set2):\n",
1877+
"def solution_sets_symmetric_difference(set1: set, set2: set) -> set:\n",
18781878
" # Your code starts here\n",
18791879
" return\n",
18801880
" # Your code ends here "
@@ -1888,7 +1888,7 @@
18881888
"source": [
18891889
"%%ipytest\n",
18901890
"\n",
1891-
"def solution_sets_subset(set1, set2):\n",
1891+
"def solution_sets_subset(set1: set, set2: set) -> set:\n",
18921892
" # Your code starts here\n",
18931893
" return\n",
18941894
" # Your code ends here"
@@ -1902,7 +1902,7 @@
19021902
"source": [
19031903
"%%ipytest\n",
19041904
"\n",
1905-
"def solution_sets_superset(set1, set2):\n",
1905+
"def solution_sets_superset(set1: set, set2: set) -> set:\n",
19061906
" # Your code starts here\n",
19071907
" return\n",
19081908
" # Your code ends here"
@@ -1918,7 +1918,7 @@
19181918
"source": [
19191919
"%%ipytest\n",
19201920
"\n",
1921-
"def solution_sets_disjoint(set1, set2):\n",
1921+
"def solution_sets_disjoint(set1: set, set2: set) -> set:\n",
19221922
" # Your code starts here\n",
19231923
" return\n",
19241924
" # Your code ends here"
@@ -2085,8 +2085,9 @@
20852085
"outputs": [],
20862086
"source": [
20872087
"%%ipytest\n",
2088-
"\n",
2089-
"def solution_dict_return_value(my_dict, key):\n",
2088+
"import typing\n",
2089+
"T = typing.Typevar(\"T\")\n",
2090+
"def solution_dict_return_value(my_dict: dict[typing.Hashable, T], key: typing.Hashable) -> typing.Optional[T]:\n",
20902091
" # Your code starts here\n",
20912092
" return\n",
20922093
" # Your code ends here"
@@ -2101,8 +2102,9 @@
21012102
"outputs": [],
21022103
"source": [
21032104
"%%ipytest\n",
2104-
"\n",
2105-
"def solution_dict_return_value_delete(my_dict, key):\n",
2105+
"import typing\n",
2106+
"T = typing.Typevar(\"T\")\n",
2107+
"def solution_dict_return_value_delete(my_dict: dict[typing.Hashable, T], key: typing.Hashable) -> typing.Optional[T]:\n",
21062108
" # Your code starts here\n",
21072109
" return\n",
21082110
" # Your code ends here"
@@ -2118,7 +2120,7 @@
21182120
"source": [
21192121
"%%ipytest\n",
21202122
"\n",
2121-
"def solution_update_one_dict_with_another(dict1, dict2):\n",
2123+
"def solution_update_one_dict_with_another(dict1: dict, dict2: dict) -> dict:\n",
21222124
" # Your code starts here\n",
21232125
" pass\n",
21242126
" # Your code ends here"
@@ -2263,7 +2265,7 @@
22632265
"source": [
22642266
"%%ipytest\n",
22652267
"\n",
2266-
"def solution_string_capitalize(my_string):\n",
2268+
"def solution_string_capitalize(my_string: str) -> str:\n",
22672269
" # Your code starts here\n",
22682270
" return\n",
22692271
" # Your code ends here"
@@ -2279,7 +2281,7 @@
22792281
"source": [
22802282
"%%ipytest\n",
22812283
"\n",
2282-
"def solution_string_lower_case(my_string):\n",
2284+
"def solution_string_lower_case(my_string: str) -> str:\n",
22832285
" # Your code starts here\n",
22842286
" return\n",
22852287
" # Your code ends here"
@@ -2295,7 +2297,7 @@
22952297
"source": [
22962298
"%%ipytest\n",
22972299
"\n",
2298-
"def solution_string_word_split(my_string):\n",
2300+
"def solution_string_word_split(my_string: str) -> str:\n",
22992301
" # Your code starts here\n",
23002302
" return\n",
23012303
" # Your code ends here"
@@ -2311,7 +2313,7 @@
23112313
"source": [
23122314
"%%ipytest\n",
23132315
"\n",
2314-
"def solution_string_join_commas(my_list):\n",
2316+
"def solution_string_join_commas(my_list: str) -> str:\n",
23152317
" # Your code starts here\n",
23162318
" return\n",
23172319
" # Your code ends here"
@@ -2327,7 +2329,7 @@
23272329
"source": [
23282330
"%%ipytest\n",
23292331
"\n",
2330-
"def solution_string_split_lines(my_string):\n",
2332+
"def solution_string_split_lines(my_string: str) -> list[str]:\n",
23312333
" # Your code starts here\n",
23322334
" return\n",
23332335
" # Your code ends here"

tutorial/common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def __init__(self, questions: list = None) -> None:
110110
def add_question(self, question: Question):
111111
"""Adds a question to the quiz."""
112112
question.question.value = (
113-
f"""<strong>Q{self.nquestions+1}:</strong> """ + question.question.value
113+
f"""<strong>Q{self.nquestions + 1}:</strong> """ + question.question.value
114114
)
115115
self.questions.append(question)
116116
self.children = self.questions + self.aux

0 commit comments

Comments
 (0)