Ted Idzikowski Ted Idzikowski - 2 months ago 17
Python Question

Function that returns the max element over all even columns in matrices

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]])

Here's my attempted trial:

for j in range(ncols):
for i in range(nrows):
if i==0 or i%2 == 0:
maxf= max(sums)
return maxf


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]