mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-19 19:03:02 +08:00
Added doctest and more explanation about Dijkstra execution. (#1014)
* Added doctest and more explanation about Dijkstra execution. * tests were not passing with python2 due to missing __init__.py file at number_theory folder * Removed the dot at the beginning of the imported modules names because 'python3 -m doctest -v data_structures/hashing/*.py' and 'python3 -m doctest -v data_structures/stacks/*.py' were failing not finding hash_table.py and stack.py modules. * Moved global code to main scope and added doctest for project euler problems 1 to 14. * Added test case for negative input. * Changed N variable to do not use end of line scape because in case there is a space after it the script will break making it much more error prone. * Added problems description and doctests to the ones that were missing. Limited line length to 79 and executed python black over all scripts. * Changed the way files are loaded to support pytest call. * Added __init__.py to problems to make them modules and allow pytest execution. * Added project_euler folder to test units execution * Changed 'os.path.split(os.path.realpath(__file__))' to 'os.path.dirname()'
This commit is contained in:

committed by
cclauss

parent
2fb3beeaf1
commit
267b5eff40
@ -1,6 +1,6 @@
|
||||
from __future__ import print_function
|
||||
'''
|
||||
What is the greatest product of four adjacent numbers (horizontally, vertically, or diagonally) in this 20x20 array?
|
||||
"""
|
||||
What is the greatest product of four adjacent numbers (horizontally,
|
||||
vertically, or diagonally) in this 20x20 array?
|
||||
|
||||
08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
|
||||
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
|
||||
@ -22,47 +22,78 @@ What is the greatest product of four adjacent numbers (horizontally, vertically,
|
||||
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
|
||||
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
|
||||
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48
|
||||
'''
|
||||
"""
|
||||
|
||||
from __future__ import print_function
|
||||
import os
|
||||
|
||||
try:
|
||||
xrange #Python 2
|
||||
xrange # Python 2
|
||||
except NameError:
|
||||
xrange = range #Python 2
|
||||
xrange = range # Python 2
|
||||
|
||||
|
||||
def largest_product(grid):
|
||||
nColumns = len(grid[0])
|
||||
nRows = len(grid)
|
||||
nColumns = len(grid[0])
|
||||
nRows = len(grid)
|
||||
|
||||
largest = 0
|
||||
lrDiagProduct = 0
|
||||
rlDiagProduct = 0
|
||||
largest = 0
|
||||
lrDiagProduct = 0
|
||||
rlDiagProduct = 0
|
||||
|
||||
#Check vertically, horizontally, diagonally at the same time (only works for nxn grid)
|
||||
for i in xrange(nColumns):
|
||||
for j in xrange(nRows-3):
|
||||
vertProduct = grid[j][i]*grid[j+1][i]*grid[j+2][i]*grid[j+3][i]
|
||||
horzProduct = grid[i][j]*grid[i][j+1]*grid[i][j+2]*grid[i][j+3]
|
||||
# Check vertically, horizontally, diagonally at the same time (only works
|
||||
# for nxn grid)
|
||||
for i in xrange(nColumns):
|
||||
for j in xrange(nRows - 3):
|
||||
vertProduct = (
|
||||
grid[j][i] * grid[j + 1][i] * grid[j + 2][i] * grid[j + 3][i]
|
||||
)
|
||||
horzProduct = (
|
||||
grid[i][j] * grid[i][j + 1] * grid[i][j + 2] * grid[i][j + 3]
|
||||
)
|
||||
|
||||
#Left-to-right diagonal (\) product
|
||||
if (i < nColumns-3):
|
||||
lrDiagProduct = grid[i][j]*grid[i+1][j+1]*grid[i+2][j+2]*grid[i+3][j+3]
|
||||
# Left-to-right diagonal (\) product
|
||||
if i < nColumns - 3:
|
||||
lrDiagProduct = (
|
||||
grid[i][j]
|
||||
* grid[i + 1][j + 1]
|
||||
* grid[i + 2][j + 2]
|
||||
* grid[i + 3][j + 3]
|
||||
)
|
||||
|
||||
#Right-to-left diagonal(/) product
|
||||
if (i > 2):
|
||||
rlDiagProduct = grid[i][j]*grid[i-1][j+1]*grid[i-2][j+2]*grid[i-3][j+3]
|
||||
# Right-to-left diagonal(/) product
|
||||
if i > 2:
|
||||
rlDiagProduct = (
|
||||
grid[i][j]
|
||||
* grid[i - 1][j + 1]
|
||||
* grid[i - 2][j + 2]
|
||||
* grid[i - 3][j + 3]
|
||||
)
|
||||
|
||||
maxProduct = max(vertProduct, horzProduct, lrDiagProduct, rlDiagProduct)
|
||||
if maxProduct > largest:
|
||||
largest = maxProduct
|
||||
maxProduct = max(
|
||||
vertProduct, horzProduct, lrDiagProduct, rlDiagProduct
|
||||
)
|
||||
if maxProduct > largest:
|
||||
largest = maxProduct
|
||||
|
||||
return largest
|
||||
return largest
|
||||
|
||||
if __name__ == '__main__':
|
||||
grid = []
|
||||
with open('grid.txt') as file:
|
||||
for line in file:
|
||||
grid.append(line.strip('\n').split(' '))
|
||||
|
||||
grid = [[int(i) for i in grid[j]] for j in xrange(len(grid))]
|
||||
def solution():
|
||||
"""Returns the sum of all the multiples of 3 or 5 below n.
|
||||
|
||||
>>> solution()
|
||||
70600674
|
||||
"""
|
||||
grid = []
|
||||
with open(os.path.dirname(__file__) + "/grid.txt") as file:
|
||||
for line in file:
|
||||
grid.append(line.strip("\n").split(" "))
|
||||
|
||||
print(largest_product(grid))
|
||||
grid = [[int(i) for i in grid[j]] for j in xrange(len(grid))]
|
||||
|
||||
return largest_product(grid)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(solution())
|
||||
|
Reference in New Issue
Block a user