Right now I'm stuck at a problem and can't seem to figure it out. I have a list/tuple, which can in turn contain many lists/tuples/ints. I need to find the maximum value out of the object, and hence I need to look into all levels inside the object. As an example, I have the item:
(5, (1,2), [[1],[2]])
I need to write a function which returns me 5, which is the max value inside the object.
Few more examples:
([[2, 1, 5], [4, 2], 6], ([2, 1], (7, 7, 5), 6))
Function should return 7
((4,), (2,), (6, 2), (3, 6, 2))
Function should return 6
[[[[[[6]]]]]]
Function should return 6
Edit 1:
I did try this myself. This is how I thought to solve it.
def function1(x):
global flatList
flatList = []
for i in x:
if type(i) is int or type(i) is float:
flatList.append(i)
else:
max_val(i)
if len(flatList) != 0:
return max(flatList)
I used Python Tutor to see what is wrong with my code, and I know that every time I recursively call my function, the flatList variable is getting assigned back to an empty list, and hence, the flatList will only contain the element from the last recursive call, if I am guessing correctly. I also cannot create the flatList variable outside the function, as it is one of the requirements of the assignment that I do not create any global variables of my own.