Python: creating a grid - python

Is it possible to create a grid like below?
I didn't found anything in the forum.
#euler-project problem number 11
#In the 20 times 20 grid below,
#four numbers along a diagonal line have been marked in red.
#The product of these numbers is 26 times 63 times 78 times 14 = 1788696.
#What is the greatest product of four adjacent numbers in any direction
#(up, down, left, right, or diagonally) in the 20 times 20 grid?
import numpy
number = numpy.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]
[81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65]
[52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91]
[22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80]
[24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50]
[32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70]
[67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21]
[24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72]
[21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95]
[78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92]
[16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57]
[86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58]
[19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40]
[04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66]
[88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69]
[04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36]
[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]])
EDIT no.1:
I found numpy-array now.
x = np.array([[1, 2, 3], [4, 5, 6]], np.int32)
Is there a way to do it without the commas?
EDIT no.2:
I also found a new problem.
Python: Invalid Token
Invalid token in number 08! :)

You can define the numbers in a string and split it easily in row/columns:
nums = """\
1 2 3
4 5 6
7 8 9 10
"""
rows = [map(int, row.split()) for row in nums.splitlines()]
print rows ##> [[1, 2, 3], [4, 5, 6], [7, 8, 9, 10]]

Check out NumPy - specifically, the N-dimensional array object.

Your code example won't compile unless you put commas between the list elements.
For example, this will compile:
value = [
[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9,10,11,12]
]
If you're interested in taking strings like you show, and parsing them into a list of lists (or numpy multi-dimensional array), or if you have a list of lists or numpy array and want to print them out like you describe, you can do that too with a clever couple of list comprehensions.

What you have above does not work, e.g if pasted into a file and then run as a script, or pasted into the interpreter. I get:
SyntaxError: invalid token
Again, I suspect that what you have done is paste text (a string) containing these characters. They are not integers, and you will get nowhere unless you realize that fact.
Edit: I see...we only get "invalid syntax" if we avoid the "invalid token" error caused by the "08"
>>> import numpy
>>> number = numpy.array([[08 02 22 97]])
File "<stdin>", line 1
number = numpy.array([[08 02 22 97]])
^
SyntaxError: invalid token
>>> number = numpy.array([[18 12 22 97]])
File "<stdin>", line 1
number = numpy.array([[18 12 22 97]])
^
SyntaxError: invalid syntax

As for parsing the actual data, and you don't want to read it from a file or use sensible methods, there is always this:
s = """[[08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08]
...etc
"""
s = s.replace("]", "").replace("[", "").split()
numbers = [int(x) for x in s]
Then you got a 1d array of numbers, which you can have fun with.

Related

Greatest product of four adjacent numbers in a grid (#11 Euler)

There are already many questions on stackoverlow regarding question 11 of Euler project. However I would like to figure out what is the mistake in my code.
Here is the python code:
a=[['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'],
['81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65'],
['52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91'],
['22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80'],
['24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50'],
['32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70'],
['67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21'],
['24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72'],
['21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95'],
['78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92'],
['16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57'],
['86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58'],
['19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40'],
['04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66'],
['88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69'],
['04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36'],
['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']]
b=[]
for i in range(len(a)):
b.append(a[i][0].split(' '))
Sum=1
currentSum=1
#Loop for checking horizontally adjacent sum
for x in range(20):
for y in range(16):
for z in range(4):
currentSum*=int(b[x][y+z])
if currentSum>Sum:
Sum=currentSum
currentSum=1
#Loop for checking vertically adjacent sum
for x in range(0, 16):
for y in range(0, 20):
for z in range(0, 4):
currentSum*=int(b[x+z][y])
if currentSum>Sum:
Sum=currentSum
currentSum=1
#Loop for checking diagonally adjacent sum (\)
for x in range(0, 16):
for y in range(0, 16):
for z in range(4):
currentSum*=int(b[x+z][y+z])
if currentSum>Sum:
Sum=currentSum
currentSum=1
#Loop for checking diagonally adjacent sum(/)
for x in range(3, 20):
for y in range(3, 20):
for z in range(4):
currentSum*=int(b[x-z][y-z])
if currentSum>Sum:
Sum=currentSum
currentSum=1
print(Sum)
My approach to the question:
I had manually made the grid provided into a 20 x 1 2D list. However since I need a 20 x 20 2D list, I created a new one wherein the inner list items are the results of split function upon the first list items.
I have four nested loops, each for checking the products in one of the directions mentioned
The final answer yielded is 51267216, which according to Project Euler is wrong.
I would like to know where I am going wrong, and some guidance in this direction without revealing the answer itself.
Another thing to note is I am trying to get this solved without any additional libraries like numpy.
I've finally figured out the mistake - the problem has arisen due to the use of incorrect indexing formula in the last for loop.
The correct code is:
#Loop for checking diagonally adjacent sum
for x in range(0, 16):
for y in range(3, 20):
for z in range(4):
currentSum*=int(b[x+z][y-z]) //originally: b[x-z][x-y]
if currentSum>Sum:
Sum=currentSum
currentSum=1
b[x+z][y-z] instead of b[x-z][y-z]
To add on, here are the pictorial representations of all four loops:
1st Loop:
2nd loop:
3rd Loop:
4th loop:

How to trace what cause "IndexError: list index out of range" and how to find prod(column)?

Here is the list and I have already convert it to 20 x 20 list.. ?
import numpy as np
a = \
'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 '\
'81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65 '\
'52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91 '\
'22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80 '\
'24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50 '\
'32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70 '\
'67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21 '\
'24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72 '\
'21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95 '\
'78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92 '\
'16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57 '\
'86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58 '\
'19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40 '\
'04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66 '\
'88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69 '\
'04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36 '\
'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'
Here how I convert the list to 20x20 matrix;
# rearrange a
a = a.split(' ')
# map a.str to a.int
a = [int(x) for x in a]
# create b = 20x20 matrix
b = [[ 0 for x in range(20)]for y in range(20)]
# assign b[i][j] = a[i] and set k = i for a[i]
k = 0
for i in range(20):
for j in range(20):
b[i][j] = a[k]
k += 1
I got this error :
Traceback (most recent call last):
File "C:/Users/Me/PycharmProjects/SEMUA/Euler/Euler11.py", line 55, in <module>
if np.product(b[i:i+4][j]) > largest:
IndexError: list index out of range
which refer to this part;
#column
for j in range(0,20):
for i in range(0, 17):
if np.product(b[i:i+4][j]) > largest:
largest = np.product(b[i:i+4][j])
Why there is no same error here even though I use the same numbering;
#row
for i in range(0,20):
for j in range(0,17):
if np.product(b[i][j:j+4]) > largest:
largest = np.product(b[i][j:j+4])
I notice that it work if it is a row but not if it is a column.
How can I fix this?
How can I extract a column from my 2-D list?
First, your initialization of the array could be done much easier:
aa = np.array(list(map(int, a.split())), dtype = np.int)
aa.shape = [20, 20]
You used ][, which means that first you create a view of 4 rows, then you try to select the 19th row from this -- obviously not possible. Use ,, then your indices will refer to different axes. I don't know what do you want to do, but if you want to find the consecutive 4 elements with the highest product in your array, this gonna do for you:
largest = 0
for i in range(0, 20):
for j in range(0, 17):
if np.product(aa[i, j:j+4]) > largest:
largest = np.product(aa[i, j:j+4])
Same for columns:
largest = 0
for i in range(0, 20):
for j in range(0, 17):
if np.product(aa[j:j+4, i]) > largest:
largest = np.product(aa[j:j+4, i])

Project Euler 11 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm having some trouble with my Project Euler code in Python -- When I run through the code in my head everything seems to check out, but I'm still getting the wrong answer. I'm really new to Python, so it could be any number of things. Any suggestions? Thanks in advance!
nums = '\
08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08\n\
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00\n\
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65\n\
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91\n\
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80\n\
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50\n\
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70\n\
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21\n\
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72\n\
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95\n\
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92\n\
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57\n\
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58\n\
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40\n\
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66\n\
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69\n\
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36\n\
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16\n\
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54\n\
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48'
grid = []
diag = []
for line in nums.split('\n'):
grid.append(map(int, line.split(' ')))
i=0
j=0
while i<17:
l = grid[i][j]*grid[i+1][j+1]*grid[i+2][j+2]*grid[i+3][j+3]
diag.append(l)
i+=1
if i==17:
j+=1
i=0
l = grid[i][j]*grid[i+1][j+1]*grid[i+2][j+2]*grid[i+3][j+3]
diag.append(l)
if j==16:
break
print max(diag)
My comments are more along the lines of code review, but culminating in a full solution:
You can get rid of those ugly endline escapes with the textwrap module (implicit string concatenation would work too, but it would mean more repetitious typing and clutter):
import textwrap
nums = textwrap.dedent('''\
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
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
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''')
grid = []
Use the string builtin method, splitlines, and list comprehensions are somewhat more readable than map (as well as not needing to be wrapped with a list call to be forward compatible with Python 3):
for line in nums.splitlines():
grid.append([int(i) for i in line.split(' ')])
Now we have our data, and we can begin our search algorithm. Since the horizontals are already in rows together, we can easily search by row, and since zip stops on the shortest iterable, we can safely zip up slices of the same string starting at increasing points from the beginning without getting index errors:
def max_horizontal(grid):
return max(w * x * y * z
for r in grid
for w, x, y, z in zip(r, r[1:], r[2:], r[3:]))
Vertical is not much more tricky, but we want to transpose it like it was a matrix, and then we can use the same code around it. Expanding an iterable of iterables into zip is the same as transposing such a matrix that we can iterate over:
def max_vertical(grid):
return max(w * x * y * z
for c in zip(*grid)
for w, x, y, z in zip(c, c[1:], c[2:], c[3:]))
Diagonals are a bit more difficult, but if we get one definition right, we just reverse it. Here, we need to go row by row over a window across the matrix, so we treat the matrix like we treated the row with zip. So we step over the matrix one row at a time, looking at 4 rows each time. Next, using the slash semantic to indicate the diagonal running from bottom left to top right, in our first row, we start at the fourth element (keeping in mind, Python starts indexing at 0, so that corresponds to the [3:] slice notation below), second row, 3rd element, third row, second element, 4th row, the first element. Again, since zip stops at the end of the shortest iterable, our window doesn't run out of range of the matrix:
def max_slashdiag(g=grid):
return max(w * x * y * z
for r1, r2, r3, r4 in zip(g, g[1:], g[2:], g[3:])
for w, x, y, z in zip(r1[3:], r2[2:], r3[1:], r4))
To get the other diagonal, just reverse the corresponding row starting points:
def max_backdiag(g=grid):
return max(w * x * y * z
for r1, r2, r3, r4 in zip(g, g[1:], g[2:], g[3:])
for w, x, y, z in zip(r1, r2[1:], r3[2:], r4[3:]))
And we take the maximum of all of these functions:
max(max_horizontal(grid),
max_vertical(grid),
max_slashdiag(g=grid),
max_backdiag(g=grid))
which returns 70600674

Create Grids in Python

Question : I want to create a grid like this.
08 06 78 56 96
45 63 68 23 51
63 78 45 08 37
56 73 92 73 83
43 22 67 98 55
Once I created this grid, I want to find the product of four adjacent numbers in the same direction (up, down, left, right, or diagonally)?
How can I do this?
I searched a lot and found an answer. They suggested to use N-dimensional array. But I don't know how to do that?
When you specify a backslash at the end of the string, it will allow you to continue the current string along to the next line. So let's first think about the simplest way we can stuff that grid of numbers into our code, a simple string with lines separated by newline characters!
Try doing something like this:
nums = '\
08 06 78 56 96\n\
45 63 68 23 51\n\
63 78 45 08 37\n\
56 73 92 73 83\n\
43 22 67 98 55'
grid = []
for line in nums.split('\n'):
grid.append(map(int, line.split(' ')))
And then you can print your grid with:
>>> print grid
[[8, 6, 78, 56, 96], [45, 63, 68, 23, 51], [63, 78, 45, 8, 37], [56, 73, 92, 73,
83], [43, 22, 67, 98, 55]]
The grid will be a two-dimensional list which can be queried by first specifying the row, then the column. For example if you wish to get an element from 4th row and the 2nd column, you could do:
>>> print grid[3][1] # it's 3, 1 because lists are 0-indexed
73
And that would correctly give you 73 because if you look at your original grid:
08 06 78 56 96
45 63 68 23 51
63 78 45 08 37
56 73 92 73 83
43 22 67 98 55
73 is on row 4 and column 2. Good luck with Project Euler.
Edit:
You might lack a nice source code editor so here is the Problem 11 grid from Project Euler nicely laid out in string format for you to parse and do what you please with it. :)
nums = '\
08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08\n\
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00\n\
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65\n\
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91\n\
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80\n\
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50\n\
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70\n\
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21\n\
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72\n\
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95\n\
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92\n\
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57\n\
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58\n\
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40\n\
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66\n\
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69\n\
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36\n\
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16\n\
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54\n\
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48'

Project Euler #11 in python

I am attempting to do project euler #11 (http://projecteuler.net/problem=11) , but I keep on incorrectly getting 51267216 as my answer and I can't figure out why. Right now I start by formatting the grid as a 2d array and then I iterate through all of the numbers, excluding the outside 3. Then I iterate through the 8 different directions and test for all of them. If its larger than the current largest then I store it as "top" Any help would be appreciated.
import pprint
def neg(l):
for a in l:
if a<0:
return True
return False
tg="""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
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
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"""
grid=tg.split("\n")
for a in range(0, len(grid)):
i=grid[a]
i=i.split(" ")
for b in range(0, len(i)):
i[b]=int(i[b])
grid[a]=i
top=0
for y in range(0, 19):
for x in range(0, 19):
for b in range(-1, 2):
for c in range(-1, 2):
if b+c!=0:
try:
factors=[grid[x][y],grid[x+b][y+c],grid[x+(b*2)][y+(c*2)],grid[x+(b*3)][y+(c*3)]]
if not neg(factors):
cur=grid[x][y]*grid[x+b][y+c]*grid[x+(b*2)][y+(c*2)]*grid[x+(b*3)][y+(c*3)]
if cur>top:
top=cur
except IndexError:
pass
print(top)
You have two major problems, first:
except IndexError:
doesn't catch all invalid quadruples, since Python lists allow negative indexing (list[-k] is list[len(list)-k]), so you also check quadruples wrapping around the end of the grid.
Second,
if b+c!=0:
excludes a direction from being considered at all - the direction in which the maximal product is located, at that. Find the correct condition to exclude (0,0).
Third (yes, I know I said two above, but this isn't major, perhaps),
for b in range(-1, 2):
for c in range(-1, 2):
checks the same direction twice, once for (b,c) and once for (-b,-c).
This is too long but it answer in 0.003 second
import time
start_time = time.time()
num = "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 \
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65 \
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91 \
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80 \
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50 \
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70 \
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21 \
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72 \
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95 \
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92 \
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57 \
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58 \
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40 \
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66 \
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69 \
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36 \
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"
def product(num):
rightup = 0
right = 0
rightdown = 0
down = 0
if not (17 <= i <= 19 or 37 <= i <= 39 or 57 <= i <= 59 or 77 <= i <= 79 \
or 97 <= i <= 99 or 117 <= i <= 119 or 137 <= i <= 139 or 157 <= i <= 159 \
or 177 <= i <= 179 or 197 <= i <= 199 or 217 <= i <= 219 or 237 <= i <= 239 \
or 257 <= i <= 259 or 277 <= i <= 279 or 297 <= i <= 299 or 317 <= i <= 319 \
or 337 <= i <= 339 or 357 <= i <= 359 or 377 <= i <= 379 or 397 <= i <= 399):
if i <= 339:
rightdown = my_list[i] * my_list[i + 21] * my_list[i + 42] * my_list[i + 63]
right = my_list[i] * my_list[i+1] * my_list[i+2] * my_list[i+3]
if i >= 60:
rightup = my_list[i] * my_list[i-19] * my_list[i-38] * my_list[i-57]
greatest_product = 0
if i <= 339:
down = my_list[i] * my_list[i+20] * my_list[i+40] * my_list[i+60]
greatest_product = greatest_product
return max(rightdown,right,rightup,down)
my_list = []
andis = 0
while andis < len(num):
number = num[andis] + num[andis + 1]
andis += 3
my_list.append(int(number))
m = 0
for i in range(0,400):
if product(i) > m:
m = product(i)
print("the biggest priduct is: ",m)
print("this code took {} seconds".format(time.time()-start_time))

Categories