Skip to content

Commit bcfca67

Browse files
authored
[mypy] fix type annotations for all Project Euler problems (TheAlgorithms#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
1 parent e311b02 commit bcfca67

File tree

20 files changed

+43
-26
lines changed

20 files changed

+43
-26
lines changed

project_euler/problem_003/sol1.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ def solution(n: int = 600851475143) -> int:
9292
return n
9393
for i in range(3, int(math.sqrt(n)) + 1, 2):
9494
if n % i == 0:
95-
if isprime(n / i):
96-
max_number = n / i
95+
if isprime(n // i):
96+
max_number = n // i
9797
break
9898
elif isprime(i):
9999
max_number = i

project_euler/problem_003/sol3.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def solution(n: int = 600851475143) -> int:
5757
i += 1
5858
ans = i
5959
while n % i == 0:
60-
n = n / i
60+
n = n // i
6161
i += 1
6262
return int(ans)
6363

project_euler/problem_007/sol2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def solution(nth: int = 10001) -> int:
7373
raise TypeError("Parameter nth must be int or castable to int.") from None
7474
if nth <= 0:
7575
raise ValueError("Parameter nth must be greater than or equal to one.")
76-
primes = []
76+
primes: list[int] = []
7777
num = 2
7878
while len(primes) < nth:
7979
if isprime(num):

project_euler/problem_008/sol2.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ def solution(n: str = N) -> int:
7070
"""
7171

7272
return max(
73-
reduce(lambda x, y: int(x) * int(y), n[i : i + 13]) for i in range(len(n) - 12)
73+
# mypy cannot properly interpret reduce
74+
int(reduce(lambda x, y: str(int(x) * int(y)), n[i : i + 13]))
75+
for i in range(len(n) - 12)
7476
)
7577

7678

project_euler/problem_009/sol1.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ def solution() -> int:
3636
if (a ** 2) + (b ** 2) == (c ** 2):
3737
return a * b * c
3838

39+
return -1
40+
3941

4042
def solution_fast() -> int:
4143
"""
@@ -55,6 +57,8 @@ def solution_fast() -> int:
5557
if a < b < c and (a ** 2) + (b ** 2) == (c ** 2):
5658
return a * b * c
5759

60+
return -1
61+
5862

5963
def benchmark() -> None:
6064
"""

project_euler/problem_014/sol1.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def solution(n: int = 1000000) -> int:
4444

4545
while number > 1:
4646
if number % 2 == 0:
47-
number /= 2
47+
number //= 2
4848
counter += 1
4949
else:
5050
number = (3 * number) + 1

project_euler/problem_025/sol2.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@
2323
What is the index of the first term in the Fibonacci sequence to contain 1000
2424
digits?
2525
"""
26+
from typing import Generator
2627

2728

28-
def fibonacci_generator() -> int:
29+
def fibonacci_generator() -> Generator[int, None, None]:
2930
"""
3031
A generator that produces numbers in the Fibonacci sequence
3132

project_euler/problem_026/sol1.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def solution(numerator: int = 1, digit: int = 1000) -> int:
3939
longest_list_length = 0
4040

4141
for divide_by_number in range(numerator, digit + 1):
42-
has_been_divided = []
42+
has_been_divided: list[int] = []
4343
now_divide = numerator
4444
for division_cycle in range(1, digit + 1):
4545
if now_divide in has_been_divided:

project_euler/problem_037/sol1.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def compute_truncated_primes(count: int = 11) -> list[int]:
7676
>>> compute_truncated_primes(11)
7777
[23, 37, 53, 73, 313, 317, 373, 797, 3137, 3797, 739397]
7878
"""
79-
list_truncated_primes = []
79+
list_truncated_primes: list[int] = []
8080
num = 13
8181
while len(list_truncated_primes) != count:
8282
if validate(num):

project_euler/problem_044/sol1.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ def solution(limit: int = 5000) -> int:
4242
if is_pentagonal(a) and is_pentagonal(b):
4343
return b
4444

45+
return -1
46+
4547

4648
if __name__ == "__main__":
4749
print(f"{solution() = }")

0 commit comments

Comments
 (0)