[Project Euler] Fix code style for problems 15 and 34 (#3076)

* Add type hints and default args to problem 15

* Changes function's name to solution in problem 34

* Update sol1.py

* Update sol1.py

Co-authored-by: Dhruv <dhruvmanila@gmail.com>
This commit is contained in:
Juan José Torres 2020-10-08 22:16:55 -05:00 committed by GitHub
parent 1c0deb88ac
commit 216a194e9a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 32 deletions

View File

@ -1,4 +1,6 @@
"""
Problem 15: https://projecteuler.net/problem=15
Starting in the top left corner of a 2×2 grid, and only being able to move to
the right and down, there are exactly 6 routes to the bottom right corner.
How many such routes are there through a 20×20 grid?
@ -6,34 +8,21 @@ How many such routes are there through a 20×20 grid?
from math import factorial
def lattice_paths(n):
def solution(n: int = 20) -> int:
"""
Returns the number of paths possible in a n x n grid starting at top left
corner going to bottom right corner and being able to move right and down
only.
bruno@bruno-laptop:~/git/Python/project_euler/problem_15$ python3 sol1.py 50
1.008913445455642e+29
bruno@bruno-laptop:~/git/Python/project_euler/problem_15$ python3 sol1.py 25
126410606437752.0
bruno@bruno-laptop:~/git/Python/project_euler/problem_15$ python3 sol1.py 23
8233430727600.0
bruno@bruno-laptop:~/git/Python/project_euler/problem_15$ python3 sol1.py 15
155117520.0
bruno@bruno-laptop:~/git/Python/project_euler/problem_15$ python3 sol1.py 1
2.0
>>> lattice_paths(25)
126410606437752
>>> lattice_paths(23)
8233430727600
>>> lattice_paths(20)
137846528820
>>> lattice_paths(15)
155117520
>>> lattice_paths(1)
2
Returns the number of paths possible in a n x n grid starting at top left
corner going to bottom right corner and being able to move right and down
only.
>>> solution(25)
126410606437752
>>> solution(23)
8233430727600
>>> solution(20)
137846528820
>>> solution(15)
155117520
>>> solution(1)
2
"""
n = 2 * n # middle entry of odd rows starting at row 3 is the solution for n = 1,
# 2, 3,...
@ -46,10 +35,10 @@ if __name__ == "__main__":
import sys
if len(sys.argv) == 1:
print(lattice_paths(20))
print(solution(20))
else:
try:
n = int(sys.argv[1])
print(lattice_paths(n))
print(solution(n))
except ValueError:
print("Invalid entry - please enter a number.")

View File

@ -1,4 +1,6 @@
"""
Problem 34: https://projecteuler.net/problem=34
145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145.
Find the sum of all numbers which are equal to the sum of the factorial of their digits.
Note: As 1! = 1 and 2! = 2 are not sums they are not included.
@ -18,12 +20,12 @@ def sum_of_digit_factorial(n: int) -> int:
return sum(factorial(int(char)) for char in str(n))
def compute() -> int:
def solution() -> int:
"""
Returns the sum of all numbers whose
sum of the factorials of all digits
add up to the number itself.
>>> compute()
>>> solution()
40730
"""
limit = 7 * factorial(9) + 1
@ -31,4 +33,4 @@ def compute() -> int:
if __name__ == "__main__":
print(f"{compute()} = ")
print(f"{solution()} = ")