Skip to content

Commit ca055cc

Browse files
[Project Euler] Fix code style for problems 15 and 34 (TheAlgorithms#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 <[email protected]>
1 parent 1ed905f commit ca055cc

File tree

2 files changed

+23
-32
lines changed

2 files changed

+23
-32
lines changed

Diff for: project_euler/problem_15/sol1.py

+18-29
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,28 @@
11
"""
2+
Problem 15: https://projecteuler.net/problem=15
3+
24
Starting in the top left corner of a 2×2 grid, and only being able to move to
35
the right and down, there are exactly 6 routes to the bottom right corner.
46
How many such routes are there through a 20×20 grid?
57
"""
68
from math import factorial
79

810

9-
def lattice_paths(n):
11+
def solution(n: int = 20) -> int:
1012
"""
11-
Returns the number of paths possible in a n x n grid starting at top left
12-
corner going to bottom right corner and being able to move right and down
13-
only.
14-
15-
bruno@bruno-laptop:~/git/Python/project_euler/problem_15$ python3 sol1.py 50
16-
1.008913445455642e+29
17-
bruno@bruno-laptop:~/git/Python/project_euler/problem_15$ python3 sol1.py 25
18-
126410606437752.0
19-
bruno@bruno-laptop:~/git/Python/project_euler/problem_15$ python3 sol1.py 23
20-
8233430727600.0
21-
bruno@bruno-laptop:~/git/Python/project_euler/problem_15$ python3 sol1.py 15
22-
155117520.0
23-
bruno@bruno-laptop:~/git/Python/project_euler/problem_15$ python3 sol1.py 1
24-
2.0
25-
26-
>>> lattice_paths(25)
27-
126410606437752
28-
>>> lattice_paths(23)
29-
8233430727600
30-
>>> lattice_paths(20)
31-
137846528820
32-
>>> lattice_paths(15)
33-
155117520
34-
>>> lattice_paths(1)
35-
2
36-
13+
Returns the number of paths possible in a n x n grid starting at top left
14+
corner going to bottom right corner and being able to move right and down
15+
only.
16+
>>> solution(25)
17+
126410606437752
18+
>>> solution(23)
19+
8233430727600
20+
>>> solution(20)
21+
137846528820
22+
>>> solution(15)
23+
155117520
24+
>>> solution(1)
25+
2
3726
"""
3827
n = 2 * n # middle entry of odd rows starting at row 3 is the solution for n = 1,
3928
# 2, 3,...
@@ -46,10 +35,10 @@ def lattice_paths(n):
4635
import sys
4736

4837
if len(sys.argv) == 1:
49-
print(lattice_paths(20))
38+
print(solution(20))
5039
else:
5140
try:
5241
n = int(sys.argv[1])
53-
print(lattice_paths(n))
42+
print(solution(n))
5443
except ValueError:
5544
print("Invalid entry - please enter a number.")

Diff for: project_euler/problem_34/sol1.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
"""
2+
Problem 34: https://projecteuler.net/problem=34
3+
24
145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145.
35
Find the sum of all numbers which are equal to the sum of the factorial of their digits.
46
Note: As 1! = 1 and 2! = 2 are not sums they are not included.
@@ -18,17 +20,17 @@ def sum_of_digit_factorial(n: int) -> int:
1820
return sum(factorial(int(char)) for char in str(n))
1921

2022

21-
def compute() -> int:
23+
def solution() -> int:
2224
"""
2325
Returns the sum of all numbers whose
2426
sum of the factorials of all digits
2527
add up to the number itself.
26-
>>> compute()
28+
>>> solution()
2729
40730
2830
"""
2931
limit = 7 * factorial(9) + 1
3032
return sum(i for i in range(3, limit) if sum_of_digit_factorial(i) == i)
3133

3234

3335
if __name__ == "__main__":
34-
print(f"{compute()} = ")
36+
print(f"{solution()} = ")

0 commit comments

Comments
 (0)