Add type hints in problems 11-20

This commit is contained in:
daniele 2024-09-29 15:04:13 +02:00
parent 2d3b9eb45b
commit 5ed4a6d9be
Signed by: fuxino
GPG Key ID: 981A2B2A3BBF5514
10 changed files with 16 additions and 20 deletions

View File

@ -31,7 +31,7 @@ from projecteuler import timing
@timing @timing
def p011(): def p011() -> None:
grid = [[8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8], grid = [[8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8],
[49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0], [49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0],
[81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36, 65], [81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36, 65],
@ -67,8 +67,7 @@ def p011():
prod = prod * grid[i][k] prod = prod * grid[i][k]
k = k + 1 k = k + 1
if prod > max_: max_ = max(max_, prod)
max_ = prod
# Vertical direction. # Vertical direction.
prod = 1 prod = 1
@ -77,8 +76,7 @@ def p011():
prod = prod * grid[k][j] prod = prod * grid[k][j]
k = k + 1 k = k + 1
if prod > max_: max_ = max(max_, prod)
max_ = prod
# Diagonal direction, from top left to bottom right. # Diagonal direction, from top left to bottom right.
prod = 1 prod = 1
@ -90,8 +88,7 @@ def p011():
k = k + 1 k = k + 1
w = w + 1 w = w + 1
if prod > max_: max_ = max(max_, prod)
max_ = prod
# The last diagonal is handled separately # The last diagonal is handled separately
for i in range(17): for i in range(17):
@ -106,8 +103,7 @@ def p011():
k = k + 1 k = k + 1
w = w - 1 w = w - 1
if prod > max_: max_ = max(max_, prod)
max_ = prod
print('Project Euler, Problem 11') print('Project Euler, Problem 11')
print(f'Answer: {max_}') print(f'Answer: {max_}')

View File

@ -23,7 +23,7 @@ from projecteuler import count_divisors, timing
@timing @timing
def p012(): def p012() -> None:
i = 0 i = 0
triang = 0 triang = 0
finished = 0 finished = 0

View File

@ -109,7 +109,7 @@ from projecteuler import timing
@timing @timing
def p013(): def p013() -> None:
numbers = [37107287533902102798797998220837590246510135740250, numbers = [37107287533902102798797998220837590246510135740250,
46376937677490009712648124896970078050417018260538, 46376937677490009712648124896970078050417018260538,
74324986199524741059474233309513058123726617309629, 74324986199524741059474233309513058123726617309629,

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# The following iterative sequence is defined for the set of positive integers: # The following iterative sequence is defined for the set of positive integers:
# #
@ -28,7 +28,7 @@ collatz_found = zeros(N, dtype=int)
# Recursive function to calculate the Collatz sequence for n. # Recursive function to calculate the Collatz sequence for n.
# If n is even, Collatz(n)=1+Collatz(n/2), if n is odd # If n is even, Collatz(n)=1+Collatz(n/2), if n is odd
# Collatz(n)=1+Collatz(3*n+1). # Collatz(n)=1+Collatz(3*n+1).
def collatz_length(n): def collatz_length(n: int) -> int:
if n == 1: if n == 1:
return 1 return 1
@ -45,7 +45,7 @@ def collatz_length(n):
@timing @timing
def p014(): def p014() -> None:
max_l = 0 max_l = 0
max_ = 0 max_ = 0

View File

@ -9,7 +9,7 @@ from projecteuler import timing
@timing @timing
def p015(): def p015() -> None:
# Using a combinatorial solution: in a 20x20 grid there will always be # Using a combinatorial solution: in a 20x20 grid there will always be
# 20 movements to the right and 20 movements down, that can be represented # 20 movements to the right and 20 movements down, that can be represented
# as a string of Rs and Ds. The number of routes is the number of combinations. # as a string of Rs and Ds. The number of routes is the number of combinations.

View File

@ -8,7 +8,7 @@ from projecteuler import timing
@timing @timing
def p016(): def p016() -> None:
# Simply calculate 2^1000, convert the result to string and calculate # Simply calculate 2^1000, convert the result to string and calculate
# the sum of the digits # the sum of the digits
res = str(2 ** 1000) res = str(2 ** 1000)

View File

@ -11,7 +11,7 @@ from projecteuler import timing
@timing @timing
def p017(): def p017() -> None:
# First list contains number of letters for numbers from 1 to 19, # First list contains number of letters for numbers from 1 to 19,
# the second letters for "twenty", "thirty", ..., "ninety", # the second letters for "twenty", "thirty", ..., "ninety",
# the third letters for "one hundred and", "two hundred and", ..., "nine hundre and", # the third letters for "one hundred and", "two hundred and", ..., "nine hundre and",

View File

@ -36,7 +36,7 @@ from projecteuler import find_max_path, timing
@timing @timing
def p018(): def p018() -> None:
try: try:
with open('p018_triangle.txt', 'r', encoding='utf-8') as fp: with open('p018_triangle.txt', 'r', encoding='utf-8') as fp:
triang = [] triang = []

View File

@ -19,7 +19,7 @@ from projecteuler import timing
@timing @timing
def p019(): def p019() -> None:
count = 0 count = 0
# Use the datetime library to find out which first day of the month is a Sunday # Use the datetime library to find out which first day of the month is a Sunday

View File

@ -13,7 +13,7 @@ from projecteuler import timing
@timing @timing
def p020(): def p020() -> None:
# Calculate the factorial, convert the result to string and sum the digits. # Calculate the factorial, convert the result to string and sum the digits.
n = str(factorial(100)) n = str(factorial(100))