Skip to content

Commit a2efba5

Browse files
mindauglMaximSmolskiypre-commit-ci[bot]
authored
Add euler project problem 15 additional solution (#12774)
* Add euler project problem 15 additional solution by explicitly counting the paths. * Update sol2.py * updating DIRECTORY.md * updating DIRECTORY.md * Trigger CI * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: Maxim Smolskiy <mithridatus@mail.ru> Co-authored-by: MaximSmolskiy <MaximSmolskiy@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent c34f23e commit a2efba5

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

DIRECTORY.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,11 @@
469469

470470
## Geometry
471471
* [Geometry](geometry/geometry.py)
472+
* [Graham Scan](geometry/graham_scan.py)
473+
* [Jarvis March](geometry/jarvis_march.py)
474+
* Tests
475+
* [Test Graham Scan](geometry/tests/test_graham_scan.py)
476+
* [Test Jarvis March](geometry/tests/test_jarvis_march.py)
472477

473478
## Graphics
474479
* [Bezier Curve](graphics/bezier_curve.py)
@@ -981,6 +986,7 @@
981986
* [Sol2](project_euler/problem_014/sol2.py)
982987
* Problem 015
983988
* [Sol1](project_euler/problem_015/sol1.py)
989+
* [Sol2](project_euler/problem_015/sol2.py)
984990
* Problem 016
985991
* [Sol1](project_euler/problem_016/sol1.py)
986992
* [Sol2](project_euler/problem_016/sol2.py)

project_euler/problem_015/sol2.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""
2+
Problem 15: https://projecteuler.net/problem=15
3+
4+
Starting in the top left corner of a 2x2 grid, and only being able to move to
5+
the right and down, there are exactly 6 routes to the bottom right corner.
6+
How many such routes are there through a 20x20 grid?
7+
"""
8+
9+
10+
def solution(n: int = 20) -> int:
11+
"""
12+
Solve by explicitly counting the paths with dynamic programming.
13+
14+
>>> solution(6)
15+
924
16+
>>> solution(2)
17+
6
18+
>>> solution(1)
19+
2
20+
"""
21+
22+
counts = [[1 for _ in range(n + 1)] for _ in range(n + 1)]
23+
24+
for i in range(1, n + 1):
25+
for j in range(1, n + 1):
26+
counts[i][j] = counts[i - 1][j] + counts[i][j - 1]
27+
28+
return counts[n][n]
29+
30+
31+
if __name__ == "__main__":
32+
print(solution())

0 commit comments

Comments
 (0)