Skip to content

Commit 285620b

Browse files
despadampre-commit-ci[bot]baffelliedoardob90
authored
Reveal reference solutions (#133)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Simone Baffelli <simone.baffelli@empa.ch> Co-authored-by: edoardob90 <edoardob90@gmail.com>
1 parent 5da0ace commit 285620b

12 files changed

Lines changed: 573 additions & 324 deletions

.flake8

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ ignore =
33
E501
44
W503
55
E203
6+
TRY301
67
per-file-ignores =
78
binder/ipython_config.py:E266

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,4 @@ dmypy.json
132132
.DS_Store
133133
*_files/
134134
*.html
135+
.idea/

.mypy.ini

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[mypy]
2+
; Do not warn about missing return statements
3+
warn_no_return = False
4+
; Show error messages in context
5+
show_error_context = True
6+
; Allow untyped definitions and calls
7+
disallow_untyped_defs = False
8+
disallow_untyped_calls = False
9+
; Disable import errors
10+
disable_error_code = import

binder/ipython_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Configuration file for ipython.
22

3-
c = get_config() # noqa
3+
c = get_config() # noqa # type: ignore
44

55
# ------------------------------------------------------------------------------
66
# InteractiveShellApp(Configurable) configuration

magic_example.ipynb

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,8 @@
2222
"%%ipytest\n",
2323
"# or %%ipytest test_module_name\n",
2424
"\n",
25-
"len('a')\n",
2625
"def solution_power2(x: int) -> int:\n",
27-
" print('hi')\n",
28-
" len('b')\n",
29-
" print(len('bb'))\n",
30-
" return x ** 2\n",
31-
"len('aaa')"
26+
" return x * 2"
3227
]
3328
},
3429
{

tutorial/tests/test_control_flow.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def is_prime(num: int) -> bool:
140140
return True
141141

142142

143-
def reference_solution_find_factors(num: int) -> List[int]:
143+
def reference_find_factors(num: int) -> List[int]:
144144
"""Dumb way to find the factors of an integer"""
145145
if is_prime(num):
146146
return [1, num]
@@ -149,7 +149,7 @@ def reference_solution_find_factors(num: int) -> List[int]:
149149

150150
@pytest.mark.parametrize("num", [350, 487, 965, 816, 598, 443, 13, 17, 211])
151151
def test_find_factors(num: int, function_to_test) -> None:
152-
assert function_to_test(num) == reference_solution_find_factors(num)
152+
assert function_to_test(num) == reference_find_factors(num)
153153

154154

155155
#
@@ -162,7 +162,7 @@ def test_find_factors(num: int, function_to_test) -> None:
162162
)
163163

164164

165-
def reference_solution_find_pair(nums: List[int]) -> int:
165+
def reference_find_pair(nums: List[int]) -> int:
166166
"""Reference solution (part 1)"""
167167
complements = {}
168168
for num in nums:
@@ -173,10 +173,10 @@ def reference_solution_find_pair(nums: List[int]) -> int:
173173

174174
@pytest.mark.parametrize("nums", [nums_1, nums_2])
175175
def test_find_pair(nums: List[int], function_to_test) -> None:
176-
assert function_to_test(nums) == reference_solution_find_pair(nums)
176+
assert function_to_test(nums) == reference_find_pair(nums)
177177

178178

179-
def reference_solution_find_triplet_slow(nums: List[int]) -> int:
179+
def reference_find_triplet_slow(nums: List[int]) -> int:
180180
"""Reference solution (part 2), O(n^3)"""
181181
n = len(nums)
182182
for i in range(n - 2):
@@ -186,7 +186,7 @@ def reference_solution_find_triplet_slow(nums: List[int]) -> int:
186186
return nums[i] * nums_2[j] * nums[k]
187187

188188

189-
def reference_solution_find_triplet(nums: List[int]) -> int:
189+
def reference_find_triplet(nums: List[int]) -> int:
190190
"""Reference solution (part 2), O(n^2)"""
191191
n = len(nums)
192192
for i in range(n - 1):
@@ -201,15 +201,15 @@ def reference_solution_find_triplet(nums: List[int]) -> int:
201201

202202
@pytest.mark.parametrize("nums", [nums_1, nums_2])
203203
def test_find_triplet(nums: List[int], function_to_test) -> None:
204-
assert function_to_test(nums) == reference_solution_find_triplet(nums)
204+
assert function_to_test(nums) == reference_find_triplet(nums)
205205

206206

207207
#
208208
# Exercise 3: Cats with hats
209209
#
210210

211211

212-
def reference_solution_cats_with_hats() -> int:
212+
def reference_cats_with_hats() -> int:
213213
"""Solution with dictionaries"""
214214
cats = {i: False for i in range(1, 101)}
215215

@@ -222,7 +222,7 @@ def reference_solution_cats_with_hats() -> int:
222222

223223

224224
def test_cats_with_hats(function_to_test) -> None:
225-
assert function_to_test() == reference_solution_cats_with_hats()
225+
assert function_to_test() == reference_cats_with_hats()
226226

227227

228228
#
@@ -241,9 +241,7 @@ def parse_data(filename: str) -> List[List[int]]:
241241
trees_1, trees_2 = (parse_data(f"trees_{num}.txt") for num in (1, 2))
242242

243243

244-
def reference_solution_toboggan_p1(
245-
trees_map: List[List[int]], right: int, down: int
246-
) -> int:
244+
def reference_toboggan_p1(trees_map: List[List[int]], right: int, down: int) -> int:
247245
"""Reference solution (part 1)"""
248246
start, trees, depth, width = [0, 0], 0, len(trees_map), len(trees_map[0])
249247
while start[0] < depth:
@@ -262,18 +260,16 @@ def reference_solution_toboggan_p1(
262260
def test_toboggan_p1(
263261
trees_map: List[List[int]], right: int, down: int, function_to_test
264262
) -> None:
265-
assert function_to_test(trees_map, right, down) == reference_solution_toboggan_p1(
263+
assert function_to_test(trees_map, right, down) == reference_toboggan_p1(
266264
trees_map, right, down
267265
)
268266

269267

270-
def reference_solution_toboggan_p2(
271-
trees_map: List[List[int]], slopes: Tuple[Tuple[int]]
272-
) -> int:
268+
def reference_toboggan_p2(trees_map: List[List[int]], slopes: Tuple[Tuple[int]]) -> int:
273269
"""Reference solution (part 2)"""
274270
total = 1
275271
for right, down in slopes:
276-
total *= reference_solution_toboggan_p1(trees_map, right, down)
272+
total *= reference_toboggan_p1(trees_map, right, down)
277273
return total
278274

279275

@@ -293,6 +289,6 @@ def reference_solution_toboggan_p2(
293289
def test_toboggan_p2(
294290
trees_map: List[List[int]], slopes: Tuple[Tuple[int]], function_to_test
295291
) -> None:
296-
assert function_to_test(trees_map, slopes) == reference_solution_toboggan_p2(
292+
assert function_to_test(trees_map, slopes) == reference_toboggan_p2(
297293
trees_map, slopes
298294
)

tutorial/tests/test_functional_programming.py

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from numpy.typing import NDArray
1212

1313

14-
def reference_solution_exercise2(my_list: List[int], k: int):
14+
def reference_exercise2(my_list: List[int], k: int) -> List[int]:
1515
return [i for i in my_list if i % k == 0]
1616

1717

@@ -24,7 +24,7 @@ def check_for_loop_in_body(fun: Callable) -> bool:
2424
return False
2525

2626

27-
def reference_solution_filter_even(my_list: "list[int]") -> "list[int]":
27+
def reference_filter_even(my_list: List[int]) -> List[int]:
2828
return list(filter(lambda x: x % 2 == 0, my_list))
2929

3030

@@ -39,15 +39,15 @@ def reference_solution_filter_even(my_list: "list[int]") -> "list[int]":
3939
def test_filter_even(function_to_test: Callable, my_list: List[int]):
4040
res = function_to_test(my_list)
4141
assert type(res) == list, "The function you wrote does not return a list"
42-
assert res == reference_solution_filter_even(
42+
assert res == reference_filter_even(
4343
my_list
4444
), "The list you return is not equal to the expected solution"
4545
assert not check_for_loop_in_body(
4646
function_to_test
4747
), "You are not allowed to use a for loop in this exercise"
4848

4949

50-
def reference_solution_add_one(my_list: List[int]) -> List[int]:
50+
def reference_add_one(my_list: List[int]) -> List[int]:
5151
return list(map(lambda x: x + 1, my_list)) # noqa: C417
5252

5353

@@ -60,7 +60,7 @@ def reference_solution_add_one(my_list: List[int]) -> List[int]:
6060
],
6161
)
6262
def test_add_one(function_to_test: Callable, my_list: List[int]):
63-
assert function_to_test(my_list) == reference_solution_add_one(
63+
assert function_to_test(my_list) == reference_add_one(
6464
my_list
6565
), "The list you return is not equal to the expected solution"
6666
assert not check_for_loop_in_body(
@@ -76,14 +76,14 @@ def test_add_one(function_to_test: Callable, my_list: List[int]):
7676
],
7777
)
7878
def test_exercise2(
79-
function_to_test: Callable[[int, int], int],
79+
function_to_test: Callable[[List[int]], int],
8080
my_list: List[int],
8181
k: int,
8282
):
83-
assert function_to_test(my_list, k) == reference_solution_exercise2(my_list, k)
83+
assert function_to_test(my_list, k) == reference_exercise2(my_list, k)
8484

8585

86-
def reference_solution_exercise3(x: List[List[int]]) -> List[List[int]]:
86+
def reference_exercise3(x: List[List[int]]) -> List[List[int]]:
8787
return [list(i) for i in zip(*x)]
8888

8989

@@ -96,28 +96,27 @@ def test_exercise3(
9696
):
9797
res = function_to_test(my_input.tolist())
9898
assert (
99-
res == reference_solution_exercise3(my_input.tolist())
99+
res == reference_exercise3(my_input.tolist())
100100
and res == my_input.transpose().tolist()
101101
)
102102

103103

104-
def reference_solution_exercise4(my_list: List[List[Any]]) -> List[Any]:
104+
def reference_exercise4(my_list: List[List[Any]]) -> List[Any]:
105105
return functools.reduce(lambda x, y: x + y, my_list)
106106

107107

108108
@pytest.mark.parametrize(
109-
"my_input, reference_func",
109+
"my_input",
110110
[
111-
([[1, 2, 3, 4], [4, 5, 5], [4, 5, 6]], reference_solution_exercise4),
112-
([["a", "b", "c"], ["d", "f", "e"], ["another"]], reference_solution_exercise4),
111+
[[1, 2, 3, 4], [4, 5, 5], [4, 5, 6]],
112+
[["a", "b", "c"], ["d", "f", "e"], ["another"]],
113113
],
114114
)
115115
def test_exercise4(
116-
function_to_test: Callable[[List[List[any]]], List[Any]],
116+
function_to_test: Callable[[List[List[Any]]], List[Any]],
117117
my_input: List[List[Any]],
118-
reference_func: Callable,
119118
):
120-
assert function_to_test(my_input) == reference_func(my_input)
119+
assert function_to_test(my_input) == reference_exercise4(my_input)
121120

122121

123122
@functools.lru_cache
@@ -126,7 +125,7 @@ def get_data_exercise5() -> List[str]:
126125
return words.splitlines()
127126

128127

129-
def reference_solution_exercise5(w: List[str]) -> List[Tuple[str, int]]:
128+
def reference_exercise5(w: List[str]) -> List[Tuple[str, int]]:
130129
return [
131130
(k, len(list(v)))
132131
for k, v in itertools.groupby(sorted(w, key=lambda x: x[0]), key=lambda x: x[0])
@@ -135,21 +134,19 @@ def reference_solution_exercise5(w: List[str]) -> List[Tuple[str, int]]:
135134

136135
def test_exercise5(function_to_test: Callable[[List[str]], List[Tuple[str, int]]]):
137136
data = get_data_exercise5()
138-
assert function_to_test(data) == reference_solution_exercise5(data)
137+
assert function_to_test(data) == reference_exercise5(data)
139138

140139

141-
def reference_solution_exercise6(
142-
my_list: List[Tuple[str, int]]
143-
) -> List[Tuple[str, float]]:
140+
def reference_exercise6(my_list: List[Tuple[str, int]]) -> List[Tuple[str, float]]:
144141
total = sum(map(lambda x: x[1], my_list)) # noqa: C417
145142
return [(letter, freq / total) for letter, freq in my_list]
146143

147144

148145
def test_exercise6(
149146
function_to_test: Callable[[List[Tuple[str, int]]], List[Tuple[str, float]]]
150147
):
151-
input_data = reference_solution_exercise5(get_data_exercise5())
152-
assert function_to_test(input_data) == reference_solution_exercise6(input_data)
148+
input_data = reference_exercise5(get_data_exercise5())
149+
assert function_to_test(input_data) == reference_exercise6(input_data)
153150

154151

155152
def reference_function_exercise7(my_list: List[str]) -> List[str]:

0 commit comments

Comments
 (0)