+ 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

9th Mar 2018, 10:04 PM
NIMA
NIMA - avatar
5 ответов
+ 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)
9th Mar 2018, 10:16 PM
Pedro Demingos
Pedro Demingos - avatar
+ 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.
12th Mar 2018, 12:44 PM
Pedro Demingos
Pedro Demingos - avatar
10th Mar 2018, 12:23 AM
foowa
foowa - avatar
10th Mar 2018, 6:18 AM
Louis
Louis - avatar
+ 1
Dear Pedro Demingos Your code is appropriate but I always receive None based on your code: https://code.sololearn.com/c4qfAfwE3j34/#py
10th Mar 2018, 12:48 PM
NIMA
NIMA - avatar