Use same conventions for variables names
This commit is contained in:
parent
5ed4a6d9be
commit
d943c32ea3
@ -9,16 +9,16 @@ from projecteuler import timing
|
|||||||
|
|
||||||
@timing
|
@timing
|
||||||
def p001() -> None:
|
def p001() -> None:
|
||||||
sum_ = 0
|
_sum = 0
|
||||||
|
|
||||||
# Simple brute-force approach: try every number between 3 and 999,
|
# Simple brute-force approach: try every number between 3 and 999,
|
||||||
# check if it's a multiple of 3 or 5, if yes add it to the total.
|
# check if it's a multiple of 3 or 5, if yes add it to the total.
|
||||||
for i in range(3, 1000):
|
for i in range(3, 1000):
|
||||||
if i % 3 == 0 or i % 5 == 0:
|
if i % 3 == 0 or i % 5 == 0:
|
||||||
sum_ += i
|
_sum += i
|
||||||
|
|
||||||
print('Project Euler, Problem 1')
|
print('Project Euler, Problem 1')
|
||||||
print(f'Answer: {sum_}')
|
print(f'Answer: {_sum}')
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
@ -16,20 +16,20 @@ def p002() -> None:
|
|||||||
fib1 = 1
|
fib1 = 1
|
||||||
fib2 = 2
|
fib2 = 2
|
||||||
fibn = fib1 + fib2
|
fibn = fib1 + fib2
|
||||||
sum_ = 2
|
_sum = 2
|
||||||
|
|
||||||
# Simple brute-force approach: generate every value in the Fibonacci
|
# Simple brute-force approach: generate every value in the Fibonacci
|
||||||
# sequence smaller than 4 million and if it's even add it to the total.
|
# sequence smaller than 4 million and if it's even add it to the total.
|
||||||
while fibn < N:
|
while fibn < N:
|
||||||
if fibn % 2 == 0:
|
if fibn % 2 == 0:
|
||||||
sum_ = sum_ + fibn
|
_sum = _sum + fibn
|
||||||
|
|
||||||
fib1 = fib2
|
fib1 = fib2
|
||||||
fib2 = fibn
|
fib2 = fibn
|
||||||
fibn = fib1 + fib2
|
fibn = fib1 + fib2
|
||||||
|
|
||||||
print('Project Euler, Problem 2')
|
print('Project Euler, Problem 2')
|
||||||
print(f'Answer: {sum_}')
|
print(f'Answer: {_sum}')
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
@ -9,7 +9,7 @@ from projecteuler import is_palindrome, timing
|
|||||||
|
|
||||||
@timing
|
@timing
|
||||||
def p004() -> None:
|
def p004() -> None:
|
||||||
max_ = 0
|
_max = 0
|
||||||
|
|
||||||
# Using a brute-force approach: generate every product of 3-digit numbers
|
# Using a brute-force approach: generate every product of 3-digit numbers
|
||||||
# and check if it's palindrome. If the product found is greater than the
|
# and check if it's palindrome. If the product found is greater than the
|
||||||
@ -18,11 +18,11 @@ def p004() -> None:
|
|||||||
for j in range(i, 99, -1):
|
for j in range(i, 99, -1):
|
||||||
num = i * j
|
num = i * j
|
||||||
# Use the function defined in projecteuler.py to check if a number is palindrome.
|
# Use the function defined in projecteuler.py to check if a number is palindrome.
|
||||||
if num > max_ and is_palindrome(num, 10):
|
if num > _max and is_palindrome(num, 10):
|
||||||
max_ = num
|
_max = num
|
||||||
|
|
||||||
print('Project Euler, Problem 4')
|
print('Project Euler, Problem 4')
|
||||||
print(f'Answer: {max_}')
|
print(f'Answer: {_max}')
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
@ -14,15 +14,15 @@ def p010() -> None:
|
|||||||
# Use the function in projecteuler.py implementing the
|
# Use the function in projecteuler.py implementing the
|
||||||
# Sieve of Eratosthenes algorithm to generate primes.
|
# Sieve of Eratosthenes algorithm to generate primes.
|
||||||
primes = sieve(N)
|
primes = sieve(N)
|
||||||
sum_ = 0
|
_sum = 0
|
||||||
|
|
||||||
# Sum all the primes
|
# Sum all the primes
|
||||||
for i in range(N):
|
for i in range(N):
|
||||||
if primes[i] == 1:
|
if primes[i] == 1:
|
||||||
sum_ = sum_ + i
|
_sum = _sum + i
|
||||||
|
|
||||||
print('Project Euler, Problem 10')
|
print('Project Euler, Problem 10')
|
||||||
print(f'Answer: {sum_}')
|
print(f'Answer: {_sum}')
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
@ -53,7 +53,7 @@ def p011() -> None:
|
|||||||
[20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54],
|
[20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54],
|
||||||
[1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48]]
|
[1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48]]
|
||||||
|
|
||||||
max_ = 0
|
_max = 0
|
||||||
|
|
||||||
# Brute-force approach: for each number in the grid, try products with its three
|
# Brute-force approach: for each number in the grid, try products with its three
|
||||||
# adjacent numbers in every direction (horizontal, vertical and the two diagonals).
|
# adjacent numbers in every direction (horizontal, vertical and the two diagonals).
|
||||||
@ -67,7 +67,7 @@ def p011() -> None:
|
|||||||
prod = prod * grid[i][k]
|
prod = prod * grid[i][k]
|
||||||
k = k + 1
|
k = k + 1
|
||||||
|
|
||||||
max_ = max(max_, prod)
|
_max = max(_max, prod)
|
||||||
|
|
||||||
# Vertical direction.
|
# Vertical direction.
|
||||||
prod = 1
|
prod = 1
|
||||||
@ -76,7 +76,7 @@ def p011() -> None:
|
|||||||
prod = prod * grid[k][j]
|
prod = prod * grid[k][j]
|
||||||
k = k + 1
|
k = k + 1
|
||||||
|
|
||||||
max_ = max(max_, prod)
|
_max = max(_max, prod)
|
||||||
|
|
||||||
# Diagonal direction, from top left to bottom right.
|
# Diagonal direction, from top left to bottom right.
|
||||||
prod = 1
|
prod = 1
|
||||||
@ -88,7 +88,7 @@ def p011() -> None:
|
|||||||
k = k + 1
|
k = k + 1
|
||||||
w = w + 1
|
w = w + 1
|
||||||
|
|
||||||
max_ = max(max_, prod)
|
_max = max(_max, prod)
|
||||||
|
|
||||||
# The last diagonal is handled separately
|
# The last diagonal is handled separately
|
||||||
for i in range(17):
|
for i in range(17):
|
||||||
@ -103,10 +103,10 @@ def p011() -> None:
|
|||||||
k = k + 1
|
k = k + 1
|
||||||
w = w - 1
|
w = w - 1
|
||||||
|
|
||||||
max_ = max(max_, prod)
|
_max = max(_max, prod)
|
||||||
|
|
||||||
print('Project Euler, Problem 11')
|
print('Project Euler, Problem 11')
|
||||||
print(f'Answer: {max_}')
|
print(f'Answer: {_max}')
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
@ -214,10 +214,10 @@ def p013() -> None:
|
|||||||
# Convert the list of numbers in a numpy array and calculate the sum
|
# Convert the list of numbers in a numpy array and calculate the sum
|
||||||
numbers = np.array(numbers)
|
numbers = np.array(numbers)
|
||||||
|
|
||||||
sum_ = str(numbers.sum())
|
_sum = str(numbers.sum())
|
||||||
|
|
||||||
print('Project Euler, Problem 13')
|
print('Project Euler, Problem 13')
|
||||||
print(f'Answer: {sum_[:10]}')
|
print(f'Answer: {_sum[:10]}')
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
@ -47,7 +47,7 @@ def collatz_length(n: int) -> int:
|
|||||||
@timing
|
@timing
|
||||||
def p014() -> None:
|
def p014() -> None:
|
||||||
max_l = 0
|
max_l = 0
|
||||||
max_ = 0
|
_max = 0
|
||||||
|
|
||||||
# For each number from 1 to 1000000, find the length of the sequence
|
# For each number from 1 to 1000000, find the length of the sequence
|
||||||
# and save its value, so that it can be used for the next numbers.
|
# and save its value, so that it can be used for the next numbers.
|
||||||
@ -57,10 +57,10 @@ def p014() -> None:
|
|||||||
|
|
||||||
if count > max_l:
|
if count > max_l:
|
||||||
max_l = count
|
max_l = count
|
||||||
max_ = i
|
_max = i
|
||||||
|
|
||||||
print('Project Euler, Problem 14')
|
print('Project Euler, Problem 14')
|
||||||
print(f'Answer: {max_}')
|
print(f'Answer: {_max}')
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
@ -13,13 +13,13 @@ def p016() -> None:
|
|||||||
# the sum of the digits
|
# the sum of the digits
|
||||||
res = str(2 ** 1000)
|
res = str(2 ** 1000)
|
||||||
|
|
||||||
sum_ = 0
|
_sum = 0
|
||||||
|
|
||||||
for i in res:
|
for i in res:
|
||||||
sum_ = sum_ + int(i)
|
_sum = _sum + int(i)
|
||||||
|
|
||||||
print('Project Euler, Problem 16')
|
print('Project Euler, Problem 16')
|
||||||
print(f'Answer: {sum_}')
|
print(f'Answer: {_sum}')
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
@ -21,11 +21,11 @@ def p017() -> None:
|
|||||||
[13, 13, 15, 14, 14, 13, 15, 15, 14],
|
[13, 13, 15, 14, 14, 13, 15, 15, 14],
|
||||||
[11]]
|
[11]]
|
||||||
|
|
||||||
sum_ = 0
|
_sum = 0
|
||||||
|
|
||||||
# Sum the letters of the first 19 numbers.
|
# Sum the letters of the first 19 numbers.
|
||||||
for i in range(19):
|
for i in range(19):
|
||||||
sum_ = sum_ + n_letters[0][i]
|
_sum = _sum + n_letters[0][i]
|
||||||
|
|
||||||
# Add the letters of the numbers from 20 to 99.
|
# Add the letters of the numbers from 20 to 99.
|
||||||
for i in range(8):
|
for i in range(8):
|
||||||
@ -37,7 +37,7 @@ def p017() -> None:
|
|||||||
for j in range(9):
|
for j in range(9):
|
||||||
n_letters[1][i] = n_letters[1][i] + n_letters[0][j]
|
n_letters[1][i] = n_letters[1][i] + n_letters[0][j]
|
||||||
|
|
||||||
sum_ = sum_ + n_letters[1][i]
|
_sum = _sum + n_letters[1][i]
|
||||||
|
|
||||||
# Add the letters of the numbers from 100 to 999.
|
# Add the letters of the numbers from 100 to 999.
|
||||||
for i in range(9):
|
for i in range(9):
|
||||||
@ -52,13 +52,13 @@ def p017() -> None:
|
|||||||
n_letters[2][i] = n_letters[2][i] + n_letters[1][j]
|
n_letters[2][i] = n_letters[2][i] + n_letters[1][j]
|
||||||
# "One hundred", "two hundred", ... don't have the "and", so remove
|
# "One hundred", "two hundred", ... don't have the "and", so remove
|
||||||
# three letters for each of them.
|
# three letters for each of them.
|
||||||
sum_ = sum_ + n_letters[2][i] - 3
|
_sum = _sum + n_letters[2][i] - 3
|
||||||
|
|
||||||
# Add "one thousand".
|
# Add "one thousand".
|
||||||
sum_ = sum_ + n_letters[3][0]
|
_sum = _sum + n_letters[3][0]
|
||||||
|
|
||||||
print('Project Euler, Problem 17')
|
print('Project Euler, Problem 17')
|
||||||
print(f'Answer: {sum_}')
|
print(f'Answer: {_sum}')
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
@ -53,10 +53,10 @@ def p018() -> None:
|
|||||||
triang[i] = list(map(int, triang[i]))
|
triang[i] = list(map(int, triang[i]))
|
||||||
|
|
||||||
# Use the function implemented in projecteuler.c to find the maximum path.
|
# Use the function implemented in projecteuler.c to find the maximum path.
|
||||||
max_ = find_max_path(triang, 15)
|
_max = find_max_path(triang, 15)
|
||||||
|
|
||||||
print('Project Euler, Problem 18')
|
print('Project Euler, Problem 18')
|
||||||
print(f'Answer: {max_}')
|
print(f'Answer: {_max}')
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
@ -17,13 +17,13 @@ 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))
|
||||||
|
|
||||||
sum_ = 0
|
_sum = 0
|
||||||
|
|
||||||
for i in n:
|
for i in n:
|
||||||
sum_ = sum_ + int(i)
|
_sum = _sum + int(i)
|
||||||
|
|
||||||
print('Project Euler, Problem 20')
|
print('Project Euler, Problem 20')
|
||||||
print(f'Answer: {sum_}')
|
print(f'Answer: {_sum}')
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
@ -500,12 +500,12 @@ def dijkstra(matrix: List[List[int]], distances: List[List[int]], m: int, n: int
|
|||||||
if j > 0 and distances[i][j] + matrix[i][j-1] < distances[i][j-1]:
|
if j > 0 and distances[i][j] + matrix[i][j-1] < distances[i][j-1]:
|
||||||
distances[i][j-1] = distances[i][j] + matrix[i][j-1]
|
distances[i][j-1] = distances[i][j] + matrix[i][j-1]
|
||||||
|
|
||||||
min_ = 999999999
|
_min = 999999999
|
||||||
|
|
||||||
for i in range(m):
|
for i in range(m):
|
||||||
for j in range(n):
|
for j in range(n):
|
||||||
if not visited[i][j] and distances[i][j] <= min_:
|
if not visited[i][j] and distances[i][j] <= _min:
|
||||||
min_ = distances[i][j]
|
_min = distances[i][j]
|
||||||
min_i = i
|
min_i = i
|
||||||
min_j = j
|
min_j = j
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user