mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-12-01 00:41:09 +00:00
Added xrange compatibility for Python 3
This commit is contained in:
parent
a4cd8978b4
commit
924d0a0552
|
@ -24,6 +24,11 @@ What is the greatest product of four adjacent numbers (horizontally, vertically,
|
||||||
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48
|
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
try:
|
||||||
|
xrange #Python 2
|
||||||
|
except NameError:
|
||||||
|
xrange = range #Python 2
|
||||||
|
|
||||||
def largest_product(grid):
|
def largest_product(grid):
|
||||||
nColumns = len(grid[0])
|
nColumns = len(grid[0])
|
||||||
nRows = len(grid)
|
nRows = len(grid)
|
||||||
|
@ -33,8 +38,8 @@ def largest_product(grid):
|
||||||
rlDiagProduct = 0
|
rlDiagProduct = 0
|
||||||
|
|
||||||
#Check vertically, horizontally, diagonally at the same time (only works for nxn grid)
|
#Check vertically, horizontally, diagonally at the same time (only works for nxn grid)
|
||||||
for i in range(nColumns):
|
for i in xrange(nColumns):
|
||||||
for j in range(nRows-3):
|
for j in xrange(nRows-3):
|
||||||
vertProduct = grid[j][i]*grid[j+1][i]*grid[j+2][i]*grid[j+3][i]
|
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]
|
horzProduct = grid[i][j]*grid[i][j+1]*grid[i][j+2]*grid[i][j+3]
|
||||||
|
|
||||||
|
@ -58,6 +63,6 @@ if __name__ == '__main__':
|
||||||
for line in file:
|
for line in file:
|
||||||
grid.append(line.strip('\n').split(' '))
|
grid.append(line.strip('\n').split(' '))
|
||||||
|
|
||||||
grid = [[int(i) for i in grid[j]] for j in range(len(grid))]
|
grid = [[int(i) for i in grid[j]] for j in xrange(len(grid))]
|
||||||
|
|
||||||
print(largest_product(grid))
|
print(largest_product(grid))
|
Loading…
Reference in New Issue
Block a user