Work in progress, 15 doesn't work like intended
This commit is contained in:
47
15/main.py
Normal file
47
15/main.py
Normal file
@@ -0,0 +1,47 @@
|
||||
import os
|
||||
import math
|
||||
import numpy as np
|
||||
|
||||
|
||||
# Given a grid with start in one corner and goal in opposite corner, find
|
||||
# the number of paths you can take from start to goal
|
||||
|
||||
|
||||
gs = [[0] * 21] * 21
|
||||
|
||||
def gridRoutes(x, y):
|
||||
res = 0
|
||||
|
||||
if gs[x][y] > 0:
|
||||
res = gs[x][y]
|
||||
|
||||
else:
|
||||
if x == 1:
|
||||
res = y + 1
|
||||
elif y == 1:
|
||||
res = x + 1
|
||||
else:
|
||||
res = gridRoutes(x - 1, y) + gridRoutes(x, y - 1)
|
||||
|
||||
gs[x][y] = res
|
||||
print(x, y, res)
|
||||
return res
|
||||
|
||||
def dumbGridRoutes(x, y):
|
||||
if x == 1:
|
||||
return y + 1
|
||||
elif y == 1:
|
||||
return x + 1
|
||||
else:
|
||||
return dumbGridRoutes(x, y - 1) + dumbGridRoutes(x - 1, y)
|
||||
|
||||
|
||||
def main():
|
||||
print("Hello, this is Patrick")
|
||||
|
||||
g = gridRoutes(3, 2)
|
||||
gd = dumbGridRoutes(3, 2)
|
||||
print(g, gd)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user