MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / solution

Function solution

project_euler/problem_015/sol2.py:10–28  ·  view source on GitHub ↗

Solve by explicitly counting the paths with dynamic programming. >>> solution(6) 924 >>> solution(2) 6 >>> solution(1) 2

(n: int = 20)

Source from the content-addressed store, hash-verified

8
9
10def 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
31if __name__ == "__main__":

Callers 1

sol2.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected