-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathtest_control_flow.py
More file actions
294 lines (227 loc) · 7.29 KB
/
test_control_flow.py
File metadata and controls
294 lines (227 loc) · 7.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import pathlib
import sys
from collections import Counter
from math import isclose, isqrt, sqrt
from typing import List, Tuple
import pytest
def read_data(name: str, data_dir: str = "data") -> pathlib.Path:
"""Read input data"""
current_module = sys.modules[__name__]
return (
pathlib.Path(current_module.__file__).parent / f"{data_dir}/{name}"
).resolve()
#
# Warm-up exercises
#
def reference_indexed_string(string: str) -> list[tuple[str, int]]:
"""Reference solution warm-up 1"""
return [(char, index) for index, char in enumerate(string)]
@pytest.mark.parametrize(
"string",
[
"manner",
"reigning",
"complaint",
"annotator",
"assessable",
"fabricated",
"organs",
"swindle",
"imminently",
"uncomfortableness",
],
)
def test_indexed_string(string: str, function_to_test) -> None:
result = function_to_test(string)
assert result is not None, "The function should return a list, not 'None'"
assert reference_indexed_string(string) == result
def reference_range_of_nums(start: int, end: int) -> list[int]:
"""Reference solution warm-up 2"""
step = 1 if start < end else -1
return list(range(start, end + step, step))
@pytest.mark.parametrize(
"start,end",
[
(1, 10),
(99, 20),
(-20, 30),
(30, 100),
(0, -30),
],
)
def test_range_of_nums(start: int, end: int, function_to_test) -> None:
assert reference_range_of_nums(start, end) == function_to_test(
start, end
), "The function returned an empty range"
def reference_sqrt_of_nums(nums: list[int]) -> list[float]:
"""Reference solution warm-up 3"""
return [sqrt(num) for num in nums if num >= 0]
@pytest.mark.parametrize(
"nums",
[
[
-704,
21,
505,
-452,
-244,
-800,
],
[
517,
-524,
-883,
-393,
100,
2,
81,
],
],
)
def test_sqrt_of_nums(nums: list[int], function_to_test) -> None:
reference, result = reference_sqrt_of_nums(nums), function_to_test(nums)
assert isinstance(result, list), "The function should return a list, not 'None'"
assert len(reference) == len(
result
), "The function should return a list of the same length"
assert all(
isclose(x, y) for x, y in zip(reference, result)
), "The function should return the square root of each number"
def reference_divide_until(num: int) -> int:
"""Reference solution warm-up 4"""
while num % 2 == 0:
num //= 2
return num
@pytest.mark.parametrize(
"num", [8134, 92337, 27836, 79264, 85954, 50557, 68360, 58765, 76419, 5864]
)
def test_divide_until(num: int, function_to_test) -> None:
assert reference_divide_until(num) == function_to_test(num)
#
# Exercise 1: Find the factors
#
def is_prime(num: int) -> bool:
"""Check if a number is prime"""
if num <= 3:
return num > 1
if num % 2 == 0 or num % 3 == 0:
return False
for i in range(5, isqrt(num) + 1, 6):
if num % i == 0 or num % (i + 2) == 0:
return False
return True
def reference_find_factors(num: int) -> List[int]:
"""Dumb way to find the factors of an integer"""
if is_prime(num):
return [1, num]
return [m for m in range(1, num + 1) if num % m == 0]
@pytest.mark.parametrize("num", [350, 487, 965, 816, 598, 443, 13, 17, 211])
def test_find_factors(num: int, function_to_test) -> None:
assert function_to_test(num) == reference_find_factors(num)
#
# Exercise 2: Find the pair/triplet
#
nums_1, nums_2 = (
[int(x) for x in read_data(f"2020_{i}.txt").read_text().splitlines()]
for i in (1, 2)
)
def reference_find_pair(nums: List[int]) -> int:
"""Reference solution (part 1)"""
complements = {}
for num in nums:
if num in complements:
return num * complements[num]
complements[2020 - num] = num
@pytest.mark.parametrize("nums", [nums_1, nums_2])
def test_find_pair(nums: List[int], function_to_test) -> None:
assert function_to_test(nums) == reference_find_pair(nums)
def reference_find_triplet_slow(nums: List[int]) -> int:
"""Reference solution (part 2), O(n^3)"""
n = len(nums)
for i in range(n - 2):
for j in range(i + 1, n - 1):
for k in range(j + 1, n):
if nums[i] + nums[j] + nums[k] == 2020:
return nums[i] * nums_2[j] * nums[k]
def reference_find_triplet(nums: List[int]) -> int:
"""Reference solution (part 2), O(n^2)"""
n = len(nums)
for i in range(n - 1):
s = set()
target_sum = 2020 - nums[i]
for j in range(i + 1, n):
last_num = target_sum - nums[j]
if last_num in s:
return nums[i] * nums[j] * last_num
s.add(nums[j])
@pytest.mark.parametrize("nums", [nums_1, nums_2])
def test_find_triplet(nums: List[int], function_to_test) -> None:
assert function_to_test(nums) == reference_find_triplet(nums)
#
# Exercise 3: Cats with hats
#
def reference_cats_with_hats() -> int:
"""Solution with dictionaries"""
cats = {i: False for i in range(1, 101)}
for loop in range(1, 101):
for cat, has_hat in cats.items():
if cat % loop == 0:
cats[cat] = not has_hat
return Counter(cats.values())[True]
def test_cats_with_hats(function_to_test) -> None:
assert function_to_test() == reference_cats_with_hats()
#
# Exercise 4: Toboggan trajectory
#
def parse_data(filename: str) -> List[List[int]]:
"""Parse a map of trees"""
input_data = read_data(filename).read_text()
return [
[1 if pos == "#" else 0 for pos in line] for line in input_data.splitlines()
]
trees_1, trees_2 = (parse_data(f"trees_{num}.txt") for num in (1, 2))
def reference_toboggan_p1(trees_map: List[List[int]], right: int, down: int) -> int:
"""Reference solution (part 1)"""
start, trees, depth, width = [0, 0], 0, len(trees_map), len(trees_map[0])
while start[0] < depth:
trees += trees_map[start[0]][start[1]]
start = [start[0] + down, (start[1] + right) % width]
return trees
@pytest.mark.parametrize(
"trees_map, right, down",
[
(trees_1, 3, 1),
(trees_2, 3, 1),
],
)
def test_toboggan_p1(
trees_map: List[List[int]], right: int, down: int, function_to_test
) -> None:
assert function_to_test(trees_map, right, down) == reference_toboggan_p1(
trees_map, right, down
)
def reference_toboggan_p2(trees_map: List[List[int]], slopes: Tuple[Tuple[int]]) -> int:
"""Reference solution (part 2)"""
total = 1
for right, down in slopes:
total *= reference_toboggan_p1(trees_map, right, down)
return total
@pytest.mark.parametrize(
"trees_map, slopes",
[
(
trees_1,
((1, 1), (3, 1), (5, 1), (7, 1), (1, 2)),
), # 9354744432
(
trees_2,
((1, 1), (3, 1), (5, 1), (7, 1), (1, 2)),
), # 1574890240
],
)
def test_toboggan_p2(
trees_map: List[List[int]], slopes: Tuple[Tuple[int]], function_to_test
) -> None:
assert function_to_test(trees_map, slopes) == reference_toboggan_p2(
trees_map, slopes
)