mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-19 19:03:02 +08:00
Simplify code by dropping support for legacy Python (#1143)
* Simplify code by dropping support for legacy Python * sort() --> sorted()
This commit is contained in:
@ -24,14 +24,8 @@ vertically, or diagonally) in this 20x20 array?
|
||||
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
|
||||
except NameError:
|
||||
xrange = range # Python 2
|
||||
|
||||
|
||||
def largest_product(grid):
|
||||
nColumns = len(grid[0])
|
||||
@ -43,8 +37,8 @@ def largest_product(grid):
|
||||
|
||||
# Check vertically, horizontally, diagonally at the same time (only works
|
||||
# for nxn grid)
|
||||
for i in xrange(nColumns):
|
||||
for j in xrange(nRows - 3):
|
||||
for i in range(nColumns):
|
||||
for j in range(nRows - 3):
|
||||
vertProduct = (
|
||||
grid[j][i] * grid[j + 1][i] * grid[j + 2][i] * grid[j + 3][i]
|
||||
)
|
||||
@ -81,7 +75,7 @@ def largest_product(grid):
|
||||
|
||||
def solution():
|
||||
"""Returns the sum of all the multiples of 3 or 5 below n.
|
||||
|
||||
|
||||
>>> solution()
|
||||
70600674
|
||||
"""
|
||||
@ -90,7 +84,7 @@ def solution():
|
||||
for line in file:
|
||||
grid.append(line.strip("\n").split(" "))
|
||||
|
||||
grid = [[int(i) for i in grid[j]] for j in xrange(len(grid))]
|
||||
grid = [[int(i) for i in grid[j]] for j in range(len(grid))]
|
||||
|
||||
return largest_product(grid)
|
||||
|
||||
|
Reference in New Issue
Block a user