1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| mat = [
[30, 1, 5, 1],
[47, 2, 2, 2],
[12, 3, 3, 3],
[0, 18, 0, 0],
[0, 0, 14, 0],
[0, 0, 0, 15],
[0, 50, 0, 0],
[0, 38, 0, 0],
[0, 0, 23, 0],
[0, 0, 28, 0],
[0, 0, 6, 0],
[7, 0, 0, 0],
[15, 0, 0, 0],
[0, 0, 0, 8],
[0, 68, 0, 0],
[0, 0, 0, 21],
[0, 121, 0, 0],
]
def isIterable(n):
try:
if isinstance(n, str): raise TypeError("%s is not iterable" % n)
iter(n)
except TypeError as e:
return False
# try
return True
#isIterable
def getMax(mat, n):
return tuple(max(mat[i][j]for i in range(n[0], n[1]+1)) for j in range(len(mat[0]))) if isIterable(n) else tuple(mat[n])
# getMax()
if __name__ == "__main__":
print("test1:", getMax(mat, 0))
print("test3:", getMax(mat, 2))
print("test5:", getMax(mat, 5))
print("test2:", getMax(mat, (0,2,5))) |
Partager