Skip to content

Commit 5839a5f

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent a219da5 commit 5839a5f

14 files changed

+121
-107
lines changed

tutorial/pandas_helpers.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ def low_med_high_bins_viz(data, column, ylabel, title, figsize=(15, 3)):
1414
["low", "med", "high"],
1515
["///", "", "\\\\\\"],
1616
pd.cut(data[column], bins=3).unique().categories.values,
17+
strict=False,
1718
):
1819
plt.axhspan(
1920
bounds.left,
@@ -45,6 +46,7 @@ def quartile_bins_viz(data, column, ylabel, title, figsize=(15, 8)):
4546
[r"$Q_1$", r"$Q_2$", r"$Q_3$", r"$Q_4$"],
4647
["\\\\\\", "", "///", "||||"],
4748
pd.qcut(data.volume, q=4).unique().categories.values,
49+
strict=False,
4850
):
4951
plt.axhspan(
5052
bounds.left,

tutorial/tests/test_01_basic_datatypes.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import copy
22
import math
3-
from typing import Any, Callable, Hashable, Iterable
3+
from collections.abc import Callable, Hashable, Iterable
4+
from typing import Any
45

56
import pytest
67

@@ -364,7 +365,7 @@ def reference_dict_return_value(my_dict: dict[Hashable, Any], key: Any) -> Any:
364365

365366

366367
@pytest.mark.parametrize(
367-
"my_dict, key", list(zip(copy.deepcopy(DICTS1), ["b"] * len(DICTS1)))
368+
"my_dict, key", list(zip(copy.deepcopy(DICTS1), ["b"] * len(DICTS1), strict=False))
368369
)
369370
def test_dict_return_value(my_dict, key, function_to_test):
370371
my_dict_to_try = my_dict.copy()
@@ -380,7 +381,7 @@ def reference_dict_return_delete_value(my_dict: dict[Hashable, Any], key: Any) -
380381

381382

382383
@pytest.mark.parametrize(
383-
"my_dict, key", list(zip(copy.deepcopy(DICTS1), ["b"] * len(DICTS1)))
384+
"my_dict, key", list(zip(copy.deepcopy(DICTS1), ["b"] * len(DICTS1), strict=False))
384385
)
385386
def test_dict_return_delete_value(my_dict, key, function_to_test):
386387
my_dict_original1 = my_dict.copy()
@@ -400,7 +401,8 @@ def reference_update_one_dict_with_another(
400401

401402

402403
@pytest.mark.parametrize(
403-
"my_dict1, my_dict2", list(zip(copy.deepcopy(DICTS1), copy.deepcopy(DICTS2)))
404+
"my_dict1, my_dict2",
405+
list(zip(copy.deepcopy(DICTS1), copy.deepcopy(DICTS2), strict=False)),
404406
)
405407
def test_update_one_dict_with_another(my_dict1, my_dict2, function_to_test):
406408
my_dict1_original1 = my_dict1.copy()

tutorial/tests/test_02_control_flow.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import contextlib
22
import pathlib
33
from math import isclose, sqrt
4-
from typing import Any, List, Optional, Tuple
4+
from typing import Any
55

66
import pytest
77

@@ -16,7 +16,7 @@ def read_data(name: str, data_dir: str = "data") -> pathlib.Path:
1616
#
1717

1818

19-
def reference_indexed_string(string: str) -> List[Tuple[str, int]]:
19+
def reference_indexed_string(string: str) -> list[tuple[str, int]]:
2020
"""Reference solution warm-up 1"""
2121
result = []
2222
for i, char in enumerate(string):
@@ -45,7 +45,7 @@ def test_indexed_string(string: str, function_to_test) -> None:
4545
assert reference_indexed_string(string) == result
4646

4747

48-
def reference_range_of_nums(start: int, end: int) -> List[int]:
48+
def reference_range_of_nums(start: int, end: int) -> list[int]:
4949
"""Reference solution warm-up 2"""
5050
step = 1 if start < end else -1
5151
return list(range(start, end + step, step))
@@ -67,7 +67,7 @@ def test_range_of_nums(start: int, end: int, function_to_test) -> None:
6767
)
6868

6969

70-
def reference_sqrt_of_nums(numbers: List[int]) -> List[float]:
70+
def reference_sqrt_of_nums(numbers: list[int]) -> list[float]:
7171
"""Reference solution warm-up 3"""
7272
result = []
7373
for num in numbers:
@@ -104,7 +104,7 @@ def test_sqrt_of_nums(nums: list[int], function_to_test) -> None:
104104
assert len(reference) == len(result), (
105105
"The function should return a list of the same length"
106106
)
107-
assert all(isclose(x, y) for x, y in zip(reference, result)), (
107+
assert all(isclose(x, y) for x, y in zip(reference, result, strict=False)), (
108108
"The function should return the square root of each number"
109109
)
110110

@@ -128,7 +128,7 @@ def test_divide_until(num: int, function_to_test) -> None:
128128
#
129129

130130

131-
def reference_filter_by_position(numbers: List[int]) -> List[int]:
131+
def reference_filter_by_position(numbers: list[int]) -> list[int]:
132132
result = set()
133133
for pos, number in enumerate(numbers, start=1):
134134
if number > pos:
@@ -149,7 +149,7 @@ def reference_filter_by_position(numbers: List[int]) -> List[int]:
149149
[10, 20, 1, 2, 3], # Mixed large and small numbers = {10, 20}
150150
],
151151
)
152-
def test_filter_by_position(numbers: List[int], function_to_test) -> None:
152+
def test_filter_by_position(numbers: list[int], function_to_test) -> None:
153153
"""Test filtering numbers by position."""
154154
assert function_to_test(numbers) == reference_filter_by_position(numbers)
155155

@@ -159,7 +159,7 @@ def test_filter_by_position(numbers: List[int], function_to_test) -> None:
159159
#
160160

161161

162-
def reference_find_even_multiple_three(numbers: List[int]) -> Optional[int]:
162+
def reference_find_even_multiple_three(numbers: list[int]) -> int | None:
163163
result = None
164164
for number in numbers:
165165
if number % 2 == 0 and number % 3 == 0:
@@ -179,7 +179,7 @@ def reference_find_even_multiple_three(numbers: List[int]) -> Optional[int]:
179179
[1, 3, 5, 7, 12], # Valid number at the end = 12
180180
],
181181
)
182-
def test_find_even_multiple_three(numbers: List[int], function_to_test) -> None:
182+
def test_find_even_multiple_three(numbers: list[int], function_to_test) -> None:
183183
"""Test finding first even multiple of 3."""
184184
assert function_to_test(numbers) == reference_find_even_multiple_three(numbers)
185185

@@ -233,7 +233,7 @@ def test_is_pure_number(text: str, function_to_test) -> None:
233233
#
234234

235235

236-
def reference_find_factors(num: int) -> List[int]:
236+
def reference_find_factors(num: int) -> list[int]:
237237
"""Reference solution to find the factors of an integer"""
238238
factors = []
239239
for m in range(1, num + 1):
@@ -257,21 +257,21 @@ def test_find_factors(num: int, function_to_test) -> None:
257257
)
258258

259259

260-
def reference_find_pair(nums: List[int]):
260+
def reference_find_pair(nums: list[int]):
261261
"""
262262
Reference solutions:
263263
- A solution with two nested loops
264264
- A solution using a dictionary and a single loop
265265
"""
266266

267-
def find_pair_with_double_loop(nums: List[int]) -> Optional[int]:
267+
def find_pair_with_double_loop(nums: list[int]) -> int | None:
268268
"""Two nested loops"""
269269
for i in nums:
270270
for j in nums:
271271
if i + j == 2020:
272272
return i * j
273273

274-
def find_pair_with_sets(nums: List[int]) -> Optional[int]:
274+
def find_pair_with_sets(nums: list[int]) -> int | None:
275275
"""Using a dictionary and a single loop"""
276276
complements = {}
277277
for num in nums:
@@ -280,7 +280,7 @@ def find_pair_with_sets(nums: List[int]) -> Optional[int]:
280280
complements[2020 - num] = num
281281

282282

283-
def __reference_find_pair(nums: List[int]) -> Optional[int]:
283+
def __reference_find_pair(nums: list[int]) -> int | None:
284284
"""Reference solution (part 1)"""
285285
complements = {}
286286
for num in nums:
@@ -290,18 +290,18 @@ def __reference_find_pair(nums: List[int]) -> Optional[int]:
290290

291291

292292
@pytest.mark.parametrize("nums", [nums_1, nums_2])
293-
def test_find_pair(nums: List[int], function_to_test) -> None:
293+
def test_find_pair(nums: list[int], function_to_test) -> None:
294294
assert function_to_test(nums) == __reference_find_pair(nums)
295295

296296

297-
def reference_find_triplet(nums: List[int]):
297+
def reference_find_triplet(nums: list[int]):
298298
"""
299299
Reference solutions:
300300
- A slow solution with three nested loops
301301
- A fast solution using only two loops
302302
"""
303303

304-
def find_triplet_slow(nums: List[int]) -> Optional[int]:
304+
def find_triplet_slow(nums: list[int]) -> int | None:
305305
"""Slow solution with a triple loop"""
306306
n = len(nums)
307307
for i in range(n - 2):
@@ -310,7 +310,7 @@ def find_triplet_slow(nums: List[int]) -> Optional[int]:
310310
if nums[i] + nums[j] + nums[k] == 2020:
311311
return nums[i] * nums[j] * nums[k]
312312

313-
def find_triplet_best(nums: List[int]) -> Optional[int]:
313+
def find_triplet_best(nums: list[int]) -> int | None:
314314
"""Fast solution with two loops"""
315315
n = len(nums)
316316
for i in range(n - 1):
@@ -323,7 +323,7 @@ def find_triplet_best(nums: List[int]) -> Optional[int]:
323323
s.add(nums[j])
324324

325325

326-
def __reference_find_triplet(nums: List[int]) -> Optional[int]:
326+
def __reference_find_triplet(nums: list[int]) -> int | None:
327327
"""Reference solution (part 2), O(n^2)"""
328328
n = len(nums)
329329
for i in range(n - 1):
@@ -337,7 +337,7 @@ def __reference_find_triplet(nums: List[int]) -> Optional[int]:
337337

338338

339339
@pytest.mark.parametrize("nums", [nums_1, nums_2])
340-
def test_find_triplet(nums: List[int], function_to_test) -> None:
340+
def test_find_triplet(nums: list[int], function_to_test) -> None:
341341
assert function_to_test(nums) == __reference_find_triplet(nums)
342342

343343

tutorial/tests/test_03_functions.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import pathlib
33
from collections import Counter
44
from string import ascii_lowercase, ascii_uppercase
5-
from typing import Any, List
5+
from typing import Any
66

77
import pytest
88

@@ -270,7 +270,7 @@ def test_combine_anything(args: Any, function_to_test) -> None:
270270
#
271271

272272

273-
def reference_longest_sequence(nums: List[int]) -> int:
273+
def reference_longest_sequence(nums: list[int]) -> int:
274274
"""
275275
Find the longest consecutive sequence of integers
276276
@@ -301,7 +301,7 @@ def reference_longest_sequence(nums: List[int]) -> int:
301301
[0, 2, 14, 12, 4, 18, 16, 8, 10, 6],
302302
],
303303
)
304-
def test_longest_sequence(input_nums: List[int], function_to_test) -> None:
304+
def test_longest_sequence(input_nums: list[int], function_to_test) -> None:
305305
assert function_to_test(input_nums) == reference_longest_sequence(input_nums)
306306

307307

@@ -312,7 +312,7 @@ def test_longest_sequence(input_nums: List[int], function_to_test) -> None:
312312
[int(x) for x in read_data("longest_10000.txt").read_text().splitlines()],
313313
],
314314
)
315-
def test_longest_sequence_best(input_nums: List[int], function_to_test) -> None:
315+
def test_longest_sequence_best(input_nums: list[int], function_to_test) -> None:
316316
assert function_to_test(input_nums) == reference_longest_sequence(input_nums)
317317

318318

tutorial/tests/test_04_input_output.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import pathlib as pl
44
import string
55
from io import StringIO
6-
from typing import Dict, List, Tuple
76

87
import pytest
98

@@ -34,7 +33,7 @@ def test_print_odd(function_to_test, n: int):
3433
assert solution_text == reference_text
3534

3635

37-
def reference_check_for_file(current_path: pl.Path, file_name: str) -> List[pl.Path]:
36+
def reference_check_for_file(current_path: pl.Path, file_name: str) -> list[pl.Path]:
3837
for file_path in list(current_path.iterdir()):
3938
if file_name == file_path.name:
4039
return True
@@ -54,7 +53,7 @@ def test_check_for_file(function_to_test, input_path: pl.Path, file_name: str):
5453
)
5554

5655

57-
def reference_find_all_files(current_path: pl.Path) -> List[pl.Path]:
56+
def reference_find_all_files(current_path: pl.Path) -> list[pl.Path]:
5857
return list(current_path.iterdir())
5958

6059

@@ -72,7 +71,7 @@ def test_count_dirs(function_to_test):
7271
assert function_to_test(path) == reference_count_dirs(path)
7372

7473

75-
def reference_read_file(input_file: pl.Path) -> List[str]:
74+
def reference_read_file(input_file: pl.Path) -> list[str]:
7675
text = input_file.open("r")
7776
lines = text.readlines()
7877
text.close()
@@ -148,7 +147,7 @@ def test_read_write_file(function_to_test, tmp_path: pl.Path):
148147
input_file.write_text(original_content)
149148

150149

151-
def reference_exercise1(input_file: pl.Path) -> Dict[str, List[str]]:
150+
def reference_exercise1(input_file: pl.Path) -> dict[str, list[str]]:
152151
my_dict = {}
153152
with open(input_file) as csv_file:
154153
reader = csv.reader(csv_file)
@@ -191,7 +190,7 @@ def test_exercise2(function_to_test, file: str):
191190
f.write_text(original_content)
192191

193192

194-
def reference_exercise3(input_file: pl.Path) -> Dict[str, int]:
193+
def reference_exercise3(input_file: pl.Path) -> dict[str, int]:
195194
my_dict = {}
196195
my_dict = dict.fromkeys(string.ascii_lowercase, 0)
197196
with open(input_file) as file_ob:
@@ -217,7 +216,7 @@ def test_exercise3(function_to_test):
217216
f.write_text(original_content)
218217

219218

220-
def reference_exercise4(english: pl.Path, dictionary: pl.Path) -> List[Tuple[str, str]]:
219+
def reference_exercise4(english: pl.Path, dictionary: pl.Path) -> list[tuple[str, str]]:
221220
english_words = english.read_text().splitlines()
222221

223222
with dictionary.open("r") as dict_file:

0 commit comments

Comments
 (0)