What is the greatest product of four adjacent numbers in any direction (up, down, left, right, or diagonally) in the 20×20 grid?
http://projecteuler.net/problem=11
I have created two dimensional tuple, LIST from the numbers.
LIST = ((8, 2, 22, ....), (49, 49, 99, ....) ...)
Here is my solution. I am not able to figure out the logical flaw.
(Edit) : My answer is 51267216 but not correct.
# For every number check diagnolly, down and right if the product exceeds max_product.
# Ignore the exception(OutOfBounds) if encountered.
max_product = 0
for i in range(20):
for j in range(20):
temp = 1
try:
for k in range(4):
temp = temp * LIST[i][j+k]
if (temp > max_product):
max_product = temp
except:
pass
temp = 1
try:
for k in range(4):
temp = temp * LIST[i+k][j]
if (temp > max_product):
max_product = temp
except:
pass
temp = 1
try:
for k in range(4):
temp = temp * LIST[i+k][j+k]
if (temp > max_product):
max_product = temp
except:
pass
print max_product
您也许忘记了两个对角线方向吗?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.