mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-27 15:01:08 +00:00
[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
This commit is contained in:
parent
e311b02e70
commit
bcfca67faa
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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)
|
||||
)
|
||||
|
||||
|
||||
|
|
|
@ -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:
|
||||
"""
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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() = }")
|
||||
|
|
|
@ -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"""
|
||||
|
|
|
@ -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())
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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() = }")
|
||||
|
|
|
@ -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__":
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -26,7 +26,7 @@ References:
|
|||
"""
|
||||
|
||||
|
||||
cache = {}
|
||||
cache: dict[tuple[int, int, int], int] = {}
|
||||
|
||||
|
||||
def _calculate(days: int, absent: int, late: int) -> int:
|
||||
|
|
|
@ -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
|
||||
|
||||
|
||||
|
|
|
@ -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):
|
||||
|
|
Loading…
Reference in New Issue
Block a user