+ 2
How may I write a code to find maximum even number in a two dimensional list ?
How may I write a code to find maximum even number in a two dimensional list ? If the list was empty or all of numbers in list was odd it should return None max () function cannot be used
5 Antworten
+ 6
# L is the 2D list
mymax = None
for row in L:
for col in row:
if col % 2 == 0:
if col > mymax:
mymax = col
EDIT: instead, try:
L = [[1,3], [5,6,7,8]]
ns = []
for row in L:
for col in row:
if col%2 == 0:
ns.append(col)
mymax = ns[0] if len(ns)>1 else None
for n in ns:
if col > mymax:
mymax = col
print(mymax)
+ 4
@NIMA ,
There was a problem in your code. Fixing it I realized there was also a problem in my original suggestion, so I edited my post. I hope it helps.
+ 2
How about this?
https://code.sololearn.com/cr2mYnJk3JLj/?ref=app
+ 1
+ 1
Dear Pedro Demingos
Your code is appropriate but I always receive None based on your code:
https://code.sololearn.com/c4qfAfwE3j34/#py