11import contextlib
22import pathlib
33from math import isclose , sqrt
4- from typing import Any , List , Optional , Tuple
4+ from typing import Any
55
66import 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
0 commit comments