Ted Idzikowski - 4 months ago 33

Python Question

How do you write a function that returns the maximum element among all the numbers that are in even columns of matrix1 i.e the maximum element among all elements in 0th, 2th, 4th etc. column. Precondition: matrix1 is a matrix filled with numbers with at least 1 row and 1 column.

The expected resulted is as follows:

`>>> maximum_among_all_even_columns([[1,1,1,1,1,1,1],[1,10,3,20,12,6,0]])`

12

Here's my attempted trial:

`ncols=len(m[0])`

nrows=len(m)

sums=[]

for j in range(ncols):

col_sum=0

for i in range(nrows):

if i==0 or i%2 == 0:

sums.append(m[i][j])

maxf= max(sums)

return maxf

Answer

This is pretty straightforward, I don't think it requires much explanation. The difficulty might be that we are used to iterating over lines then over columns, but here it is the other way round.

```
max = matrix[0][0]
for j in range(len(matrix[0])//2) :
for i in range(len(matrix)) :
if matrix[i][2*j] > max :
max = matrix[i][2*j]
```