-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathtest_02_control_flow.py
More file actions
520 lines (409 loc) · 14.4 KB
/
test_02_control_flow.py
File metadata and controls
520 lines (409 loc) · 14.4 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
import contextlib
import pathlib
from math import isclose, sqrt
from typing import Any
import pytest
def read_data(name: str, data_dir: str = "data") -> pathlib.Path:
"""Read input data"""
return (pathlib.Path(__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"""
result = []
for i, char in enumerate(string):
result.append((char, i))
return result
@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(numbers: list[int]) -> list[float]:
"""Reference solution warm-up 3"""
result = []
for num in numbers:
if num >= 0:
result.append(sqrt(num))
return result
@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, strict=False)), (
"The function should return the square root of each number"
)
def reference_divide_until(number: int) -> int:
"""Reference solution warm-up 4"""
while number % 2 == 0:
number //= 2
return number
@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: conditionals inside loops
#
def reference_filter_by_position(numbers: list[int]) -> list[int]:
result = set()
for pos, number in enumerate(numbers, start=1):
if number > pos:
result.add(number)
return sorted(result)
@pytest.mark.parametrize(
"numbers",
[
[0, 3, 1, 2], # Basic case from example = {3}
[5, 4, 3, 2, 1], # Decreasing numbers = {4, 5}
[1, 3, 5, 7, 9], # All odd numbers = {3, 5, 7, 9}
[], # Empty list = {}
[0, 0, 0], # Same numbers with none valid = {}
[2, 2, 2, 2], # Same number with one valid = {2}
[4, 4, 4, 4], # Same number, three are valid but they are duplicates = {4}
[10, 20, 1, 2, 3], # Mixed large and small numbers = {10, 20}
],
)
def test_filter_by_position(numbers: list[int], function_to_test) -> None:
"""Test filtering numbers by position."""
assert function_to_test(numbers) == reference_filter_by_position(numbers)
#
# Exercise: breaking out of loops
#
def reference_find_even_multiple_three(numbers: list[int]) -> int | None:
result = None
for number in numbers:
if number % 2 == 0 and number % 3 == 0:
result = number
break
return result
@pytest.mark.parametrize(
"numbers",
[
[1, 2, 3, 4, 6, 8], # 6 is first even multiple of 3 = 6
[1, 3, 5, 7, 9], # No even numbers = None
[12, 18, 24], # All are valid, should return the first = 12
[], # Empty list = None
[2, 4, 6, 8, 10], # Even numbers but no multiples of 3 = None
[1, 3, 5, 7, 12], # Valid number at the end = 12
],
)
def test_find_even_multiple_three(numbers: list[int], function_to_test) -> None:
"""Test finding first even multiple of 3."""
assert function_to_test(numbers) == reference_find_even_multiple_three(numbers)
#
# Exercise: using else in loops
#
def reference_is_pure_number(text: str) -> bool:
for char in text:
if char not in "1234567890":
return False
else:
return True
def is_for_else_used(function) -> bool:
import ast
import inspect
tree = ast.parse(inspect.getsource(function))
for node in ast.walk(tree):
if isinstance(node, ast.For) and node.orelse:
return True
return False
@pytest.mark.parametrize(
"text",
[
"123456", # OK
"0987654321", # OK
"", # Empty
"abc123", # Mixed characters
"0000", # All zeros
"12.34", # With decimal point
" ", # All spaces
"-123", # With negative sign
"١٢٣", # Non-ASCII digits (should return False)
],
)
def test_is_pure_number(text: str, function_to_test) -> None:
"""Test checking for pure number strings."""
assert is_for_else_used(function_to_test), "You must use a for-else construct"
assert function_to_test(text) == reference_is_pure_number(text)
#
# Exercise 1: Find the factors
#
def reference_find_factors(num: int) -> list[int]:
"""Reference solution to find the factors of an integer"""
factors = []
for m in range(1, num + 1):
if num % m == 0:
factors.append(m)
return factors
@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]):
"""
Reference solutions:
- A solution with two nested loops
- A solution using a dictionary and a single loop
"""
def find_pair_with_double_loop(nums: list[int]) -> int | None:
"""Two nested loops"""
for i in nums:
for j in nums:
if i + j == 2020:
return i * j
def find_pair_with_sets(nums: list[int]) -> int | None:
"""Using a dictionary and a single loop"""
complements = {}
for num in nums:
if num in complements:
return num * complements[num]
complements[2020 - num] = num
def __reference_find_pair(nums: list[int]) -> int | None:
"""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(nums: list[int]):
"""
Reference solutions:
- A slow solution with three nested loops
- A fast solution using only two loops
"""
def find_triplet_slow(nums: list[int]) -> int | None:
"""Slow solution with a triple loop"""
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[j] * nums[k]
def find_triplet_best(nums: list[int]) -> int | None:
"""Fast solution with two loops"""
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])
def __reference_find_triplet(nums: list[int]) -> int | None:
"""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 = dict.fromkeys(range(1, 101), False)
for loop in range(1, 101):
for cat, has_hat in cats.items():
if cat % loop == 0:
cats[cat] = not has_hat
return sum(cats.values())
def test_cats_with_hats(function_to_test) -> None:
assert function_to_test() == reference_cats_with_hats()
#
# Exercise 4: Base converter
#
def reference_base_converter(number: str, from_base: int, to_base: int) -> str:
"""Reference solution to convert a number from one base to another"""
# Validate bases
if not (2 <= from_base <= 16 and 2 <= to_base <= 16):
err = "Bases must be between 2 and 16"
raise ValueError(err)
# Handle empty input
if not number or number.strip() in ("", "-"):
err = "Invalid empty input"
raise ValueError(err)
# Same to and from bases
if from_base == to_base:
return number
# Handle negative numbers
is_negative = number.strip().startswith("-")
number = number.strip().removeprefix("-")
# Remove spaces and convert to uppercase for consistency
number = number.replace(" ", "").upper()
# Validate digits
valid_digits = "0123456789ABCDEF"
for digit in number:
if digit not in valid_digits[:from_base]:
err = f"Invalid digit '{digit}' for base {from_base}"
raise ValueError(err)
# Convert to base 10
decimal = 0
for digit in number:
decimal = decimal * from_base + valid_digits.index(digit)
# Handle 0 as a special case
if decimal == 0:
return "0"
if to_base == 10:
return str(decimal)
# Convert to target base
result = ""
while decimal > 0:
digit = decimal % to_base
result += valid_digits[digit]
decimal //= to_base
return f"-{result}" if is_negative else result
# We need a way to "disable" the use of `int()`, otherwise it's too easy
# Solution: replace `int()` with a function that raises an exception using a context manager
@contextlib.contextmanager
def block_int():
"""Context manager to block int() usage"""
original_int = int
class IntReplacement:
def __call__(self, *args, **kwargs):
import inspect
frame = inspect.currentframe()
while frame:
if frame.f_code.co_name == "solution_base_converter":
raise AssertionError("Using int() is not allowed.") # noqa: TRY003
frame = frame.f_back
return original_int(*args, **kwargs)
def __instancecheck__(self, instance: Any, /) -> bool:
return isinstance(instance, original_int)
import builtins
builtins.int = IntReplacement()
try:
yield
finally:
builtins.int = original_int
@pytest.mark.parametrize(
"number,from_base,to_base", [("42", 10, 2), ("1A", 16, 2), ("1010", 2, 16)]
)
def test_base_converter_basics(number, from_base, to_base, function_to_test):
with block_int():
expected = reference_base_converter(number, from_base, to_base)
assert function_to_test(number, from_base, to_base) == expected
@pytest.mark.parametrize(
"number,from_base,to_base", [("10 10", 2, 10), ("FF FF", 16, 2)]
)
def test_base_converter_with_spaces(number, from_base, to_base, function_to_test):
with block_int():
expected = reference_base_converter(number, from_base, to_base)
assert function_to_test(number, from_base, to_base) == expected
@pytest.mark.parametrize("number,from_base,to_base", [("-42", 10, 2), ("-FF", 16, 10)])
def test_base_converter_negative_numbers(number, from_base, to_base, function_to_test):
with block_int():
expected = reference_base_converter(number, from_base, to_base)
assert function_to_test(number, from_base, to_base) == expected
@pytest.mark.parametrize(
"number,from_base,to_base", [("ff", 16, 10), ("FF", 16, 10), ("Ff", 16, 10)]
)
def test_base_converter_case_insensitive(number, from_base, to_base, function_to_test):
with block_int():
expected = reference_base_converter(number, from_base, to_base)
assert function_to_test(number, from_base, to_base) == expected
@pytest.mark.parametrize(
"number,from_base,to_base", [("42", 1, 10), ("42", 10, 17), ("42", 0, 0)]
)
def test_base_converter_invalid_bases(number, from_base, to_base, function_to_test):
with block_int():
with pytest.raises(ValueError):
reference_base_converter(number, from_base, to_base)
with pytest.raises(ValueError):
function_to_test(number, from_base, to_base)
@pytest.mark.parametrize(
"number,from_base,to_base",
[
("2", 2, 10), # 2 not valid in base 2
("G", 16, 2), # G not valid in base 16
("9", 8, 2), # 9 not valid in base 8
],
)
def test_base_converter_invalid_digits(number, from_base, to_base, function_to_test):
with block_int():
with pytest.raises(ValueError):
function_to_test(number, from_base, to_base)
@pytest.mark.parametrize(
"number,from_base,to_base", [("", 2, 2), (" ", 2, 2), ("-", 2, 2)]
)
def test_base_converter_empty_or_invalid_input(
number, from_base, to_base, function_to_test
):
with block_int():
with pytest.raises(ValueError):
function_to_test(number, from_base, to_base)