From bcfca67faa120cc4cb42775af302d6d52d3a3f1e Mon Sep 17 00:00:00 2001 From: Joyce Date: Tue, 12 Oct 2021 00:33:44 +0800 Subject: [PATCH] [mypy] fix type annotations for all Project Euler problems (#4747) * [mypy] fix type annotations for problem003/sol1 and problem003/sol3 * [mypy] fix type annotations for project euler problem007/sol2 * [mypy] fix type annotations for project euler problem008/sol2 * [mypy] fix type annotations for project euler problem009/sol1 * [mypy] fix type annotations for project euler problem014/sol1 * [mypy] fix type annotations for project euler problem 025/sol2 * [mypy] fix type annotations for project euler problem026/sol1.py * [mypy] fix type annotations for project euler problem037/sol1 * [mypy] fix type annotations for project euler problem044/sol1 * [mypy] fix type annotations for project euler problem046/sol1 * [mypy] fix type annotations for project euler problem051/sol1 * [mypy] fix type annotations for project euler problem074/sol2 * [mypy] fix type annotations for project euler problem080/sol1 * [mypy] fix type annotations for project euler problem099/sol1 * [mypy] fix type annotations for project euler problem101/sol1 * [mypy] fix type annotations for project euler problem188/sol1 * [mypy] fix type annotations for project euler problem191/sol1 * [mypy] fix type annotations for project euler problem207/sol1 * [mypy] fix type annotations for project euler problem551/sol1 --- project_euler/problem_003/sol1.py | 4 ++-- project_euler/problem_003/sol3.py | 2 +- project_euler/problem_007/sol2.py | 2 +- project_euler/problem_008/sol2.py | 4 +++- project_euler/problem_009/sol1.py | 4 ++++ project_euler/problem_014/sol1.py | 2 +- project_euler/problem_025/sol2.py | 3 ++- project_euler/problem_026/sol1.py | 2 +- project_euler/problem_037/sol1.py | 2 +- project_euler/problem_044/sol1.py | 2 ++ project_euler/problem_046/sol1.py | 2 ++ project_euler/problem_051/sol1.py | 8 +++++--- project_euler/problem_074/sol2.py | 4 ++-- project_euler/problem_080/sol1.py | 5 +++-- project_euler/problem_099/sol1.py | 10 ++++++---- project_euler/problem_101/sol1.py | 2 +- project_euler/problem_188/sol1.py | 4 ++-- project_euler/problem_191/sol1.py | 2 +- project_euler/problem_207/sol1.py | 2 +- project_euler/problem_551/sol1.py | 3 ++- 20 files changed, 43 insertions(+), 26 deletions(-) diff --git a/project_euler/problem_003/sol1.py b/project_euler/problem_003/sol1.py index 3441dbf9e..1f3299842 100644 --- a/project_euler/problem_003/sol1.py +++ b/project_euler/problem_003/sol1.py @@ -92,8 +92,8 @@ def solution(n: int = 600851475143) -> int: return n for i in range(3, int(math.sqrt(n)) + 1, 2): if n % i == 0: - if isprime(n / i): - max_number = n / i + if isprime(n // i): + max_number = n // i break elif isprime(i): max_number = i diff --git a/project_euler/problem_003/sol3.py b/project_euler/problem_003/sol3.py index bc6f1d2f6..e13a0eb74 100644 --- a/project_euler/problem_003/sol3.py +++ b/project_euler/problem_003/sol3.py @@ -57,7 +57,7 @@ def solution(n: int = 600851475143) -> int: i += 1 ans = i while n % i == 0: - n = n / i + n = n // i i += 1 return int(ans) diff --git a/project_euler/problem_007/sol2.py b/project_euler/problem_007/sol2.py index b395c631b..20c2ddf21 100644 --- a/project_euler/problem_007/sol2.py +++ b/project_euler/problem_007/sol2.py @@ -73,7 +73,7 @@ def solution(nth: int = 10001) -> int: raise TypeError("Parameter nth must be int or castable to int.") from None if nth <= 0: raise ValueError("Parameter nth must be greater than or equal to one.") - primes = [] + primes: list[int] = [] num = 2 while len(primes) < nth: if isprime(num): diff --git a/project_euler/problem_008/sol2.py b/project_euler/problem_008/sol2.py index 7f0540263..889c3a314 100644 --- a/project_euler/problem_008/sol2.py +++ b/project_euler/problem_008/sol2.py @@ -70,7 +70,9 @@ def solution(n: str = N) -> int: """ return max( - reduce(lambda x, y: int(x) * int(y), n[i : i + 13]) for i in range(len(n) - 12) + # mypy cannot properly interpret reduce + int(reduce(lambda x, y: str(int(x) * int(y)), n[i : i + 13])) + for i in range(len(n) - 12) ) diff --git a/project_euler/problem_009/sol1.py b/project_euler/problem_009/sol1.py index c50dfeecf..83c88acf1 100644 --- a/project_euler/problem_009/sol1.py +++ b/project_euler/problem_009/sol1.py @@ -36,6 +36,8 @@ def solution() -> int: if (a ** 2) + (b ** 2) == (c ** 2): return a * b * c + return -1 + def solution_fast() -> int: """ @@ -55,6 +57,8 @@ def solution_fast() -> int: if a < b < c and (a ** 2) + (b ** 2) == (c ** 2): return a * b * c + return -1 + def benchmark() -> None: """ diff --git a/project_euler/problem_014/sol1.py b/project_euler/problem_014/sol1.py index 1745ec931..43aa4e726 100644 --- a/project_euler/problem_014/sol1.py +++ b/project_euler/problem_014/sol1.py @@ -44,7 +44,7 @@ def solution(n: int = 1000000) -> int: while number > 1: if number % 2 == 0: - number /= 2 + number //= 2 counter += 1 else: number = (3 * number) + 1 diff --git a/project_euler/problem_025/sol2.py b/project_euler/problem_025/sol2.py index ed3b54bb3..b041afd98 100644 --- a/project_euler/problem_025/sol2.py +++ b/project_euler/problem_025/sol2.py @@ -23,9 +23,10 @@ The 12th term, F12, is the first term to contain three digits. What is the index of the first term in the Fibonacci sequence to contain 1000 digits? """ +from typing import Generator -def fibonacci_generator() -> int: +def fibonacci_generator() -> Generator[int, None, None]: """ A generator that produces numbers in the Fibonacci sequence diff --git a/project_euler/problem_026/sol1.py b/project_euler/problem_026/sol1.py index 64e0bbfef..75d48df79 100644 --- a/project_euler/problem_026/sol1.py +++ b/project_euler/problem_026/sol1.py @@ -39,7 +39,7 @@ def solution(numerator: int = 1, digit: int = 1000) -> int: longest_list_length = 0 for divide_by_number in range(numerator, digit + 1): - has_been_divided = [] + has_been_divided: list[int] = [] now_divide = numerator for division_cycle in range(1, digit + 1): if now_divide in has_been_divided: diff --git a/project_euler/problem_037/sol1.py b/project_euler/problem_037/sol1.py index 5423aac37..0411ad41b 100644 --- a/project_euler/problem_037/sol1.py +++ b/project_euler/problem_037/sol1.py @@ -76,7 +76,7 @@ def compute_truncated_primes(count: int = 11) -> list[int]: >>> compute_truncated_primes(11) [23, 37, 53, 73, 313, 317, 373, 797, 3137, 3797, 739397] """ - list_truncated_primes = [] + list_truncated_primes: list[int] = [] num = 13 while len(list_truncated_primes) != count: if validate(num): diff --git a/project_euler/problem_044/sol1.py b/project_euler/problem_044/sol1.py index d3ae6476d..3b75b6a56 100644 --- a/project_euler/problem_044/sol1.py +++ b/project_euler/problem_044/sol1.py @@ -42,6 +42,8 @@ def solution(limit: int = 5000) -> int: if is_pentagonal(a) and is_pentagonal(b): return b + return -1 + if __name__ == "__main__": print(f"{solution() = }") diff --git a/project_euler/problem_046/sol1.py b/project_euler/problem_046/sol1.py index 3fdf56755..550c4c7c4 100644 --- a/project_euler/problem_046/sol1.py +++ b/project_euler/problem_046/sol1.py @@ -85,6 +85,8 @@ def compute_nums(n: int) -> list[int]: if len(list_nums) == n: return list_nums + return [] + def solution() -> int: """Return the solution to the problem""" diff --git a/project_euler/problem_051/sol1.py b/project_euler/problem_051/sol1.py index 5f607e3ff..eedb02379 100644 --- a/project_euler/problem_051/sol1.py +++ b/project_euler/problem_051/sol1.py @@ -63,12 +63,12 @@ def digit_replacements(number: int) -> list[list[int]]: >>> digit_replacements(3112) [[3002, 3112, 3222, 3332, 3442, 3552, 3662, 3772, 3882, 3992]] """ - number = str(number) + number_str = str(number) replacements = [] digits = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"] - for duplicate in Counter(number) - Counter(set(number)): - family = [int(number.replace(duplicate, digit)) for digit in digits] + for duplicate in Counter(number_str) - Counter(set(number_str)): + family = [int(number_str.replace(duplicate, digit)) for digit in digits] replacements.append(family) return replacements @@ -106,6 +106,8 @@ def solution(family_length: int = 8) -> int: return min(primes_in_family) + return -1 + if __name__ == "__main__": print(solution()) diff --git a/project_euler/problem_074/sol2.py b/project_euler/problem_074/sol2.py index 689593277..55e67c6b9 100644 --- a/project_euler/problem_074/sol2.py +++ b/project_euler/problem_074/sol2.py @@ -20,8 +20,8 @@ counter increases. """ -factorial_cache = {} -factorial_sum_cache = {} +factorial_cache: dict[int, int] = {} +factorial_sum_cache: dict[int, int] = {} def factorial(a: int) -> int: diff --git a/project_euler/problem_080/sol1.py b/project_euler/problem_080/sol1.py index 517be3fc0..916998bdd 100644 --- a/project_euler/problem_080/sol1.py +++ b/project_euler/problem_080/sol1.py @@ -26,8 +26,8 @@ def solution() -> int: sqrt_number = number.sqrt(decimal_context) if len(str(sqrt_number)) > 1: answer += int(str(sqrt_number)[0]) - sqrt_number = str(sqrt_number)[2:101] - answer += sum(int(x) for x in sqrt_number) + sqrt_number_str = str(sqrt_number)[2:101] + answer += sum(int(x) for x in sqrt_number_str) return answer @@ -35,3 +35,4 @@ if __name__ == "__main__": import doctest doctest.testmod() + print(f"{solution() = }") diff --git a/project_euler/problem_099/sol1.py b/project_euler/problem_099/sol1.py index 88912e1f0..bf5621c65 100644 --- a/project_euler/problem_099/sol1.py +++ b/project_euler/problem_099/sol1.py @@ -22,12 +22,14 @@ def solution(data_file: str = "base_exp.txt") -> int: >>> solution() 709 """ - largest = [0, 0] + largest: float = 0 + result = 0 for i, line in enumerate(open(os.path.join(os.path.dirname(__file__), data_file))): a, x = list(map(int, line.split(","))) - if x * log10(a) > largest[0]: - largest = [x * log10(a), i + 1] - return largest[1] + if x * log10(a) > largest: + largest = x * log10(a) + result = i + 1 + return result if __name__ == "__main__": diff --git a/project_euler/problem_101/sol1.py b/project_euler/problem_101/sol1.py index 553f8f442..14013c435 100644 --- a/project_euler/problem_101/sol1.py +++ b/project_euler/problem_101/sol1.py @@ -202,7 +202,7 @@ def solution(func: Callable[[int], int] = question_function, order: int = 10) -> ] ret: int = 0 - poly: int + poly: Callable[[int], int] x_val: int for poly in polynomials: diff --git a/project_euler/problem_188/sol1.py b/project_euler/problem_188/sol1.py index 6473c6362..c8cd9eb10 100644 --- a/project_euler/problem_188/sol1.py +++ b/project_euler/problem_188/sol1.py @@ -19,7 +19,7 @@ References: """ -# small helper function for modular exponentiation +# small helper function for modular exponentiation (fast exponentiation algorithm) def _modexpt(base: int, exponent: int, modulo_value: int) -> int: """ Returns the modular exponentiation, that is the value @@ -36,7 +36,7 @@ def _modexpt(base: int, exponent: int, modulo_value: int) -> int: if exponent == 1: return base if exponent % 2 == 0: - x = _modexpt(base, exponent / 2, modulo_value) % modulo_value + x = _modexpt(base, exponent // 2, modulo_value) % modulo_value return (x * x) % modulo_value else: return (base * _modexpt(base, exponent - 1, modulo_value)) % modulo_value diff --git a/project_euler/problem_191/sol1.py b/project_euler/problem_191/sol1.py index 38325b363..6bff9d54e 100644 --- a/project_euler/problem_191/sol1.py +++ b/project_euler/problem_191/sol1.py @@ -26,7 +26,7 @@ References: """ -cache = {} +cache: dict[tuple[int, int, int], int] = {} def _calculate(days: int, absent: int, late: int) -> int: diff --git a/project_euler/problem_207/sol1.py b/project_euler/problem_207/sol1.py index fb901fde1..99d1a9174 100644 --- a/project_euler/problem_207/sol1.py +++ b/project_euler/problem_207/sol1.py @@ -90,7 +90,7 @@ def solution(max_proportion: float = 1 / 12345) -> int: perfect_partitions += 1 if perfect_partitions > 0: if perfect_partitions / total_partitions < max_proportion: - return partition_candidate + return int(partition_candidate) integer += 1 diff --git a/project_euler/problem_551/sol1.py b/project_euler/problem_551/sol1.py index 71956691a..005d2e985 100644 --- a/project_euler/problem_551/sol1.py +++ b/project_euler/problem_551/sol1.py @@ -12,9 +12,10 @@ You are given a(10^6) = 31054319. Find a(10^15) """ + ks = [k for k in range(2, 20 + 1)] base = [10 ** k for k in range(ks[-1] + 1)] -memo = {} +memo: dict[int, dict[int, list[list[int]]]] = {} def next_term(a_i, k, i, n):