mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-06-07 14:42:41 -04:00
reorganize dir
Signed-off-by: Mia Steinkirch <mia.steinkirch@gmail.com>
This commit is contained in:
parent
1b6f705e7c
commit
a8e71c50db
276 changed files with 23954 additions and 0 deletions
42
other_resources/Project-Euler/015-lattice_paths.py
Normal file
42
other_resources/Project-Euler/015-lattice_paths.py
Normal file
|
@ -0,0 +1,42 @@
|
|||
#!/usr/bin/python3
|
||||
# mari von steinkirch @2013
|
||||
# steinkirch at gmail
|
||||
|
||||
def lattice_paths(squares):
|
||||
gridsize = squares+1
|
||||
grid = [[0 for i in range(gridsize)] for j in range(gridsize)]
|
||||
row, col = 0, 0
|
||||
|
||||
while col < gridsize:
|
||||
while row < gridsize:
|
||||
|
||||
if row == 0 and col == 0:
|
||||
grid[row][col] = 1
|
||||
|
||||
else:
|
||||
if row == 0 and col != 0:
|
||||
grid[row][col] += grid[row][col-1]
|
||||
elif row != 0 and col == 0:
|
||||
grid[row][col] += grid[row-1][col]
|
||||
else:
|
||||
grid[row][col] += grid[row][col-1] + grid[row-1][col]
|
||||
|
||||
row += 1
|
||||
row = 0
|
||||
col += 1
|
||||
return grid[gridsize-1][gridsize-1]
|
||||
|
||||
|
||||
def main():
|
||||
import time
|
||||
start = time.time()
|
||||
|
||||
assert(lattice_paths(2) == 6)
|
||||
print(lattice_paths(20))
|
||||
|
||||
elapsed = (time.time() - start)
|
||||
print('Tests Passed!\n It took %s seconds to run them.' % (elapsed))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue