forked from slanj/pythonproj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmax_val.py
29 lines (26 loc) · 816 Bytes
/
max_val.py
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
def max_val(t):
""" t, tuple or list
Each element of t is either an int, a tuple, or a list
No tuple or list is empty
Returns the maximum int in t or (recursively) in an element of t """
# Your code here
flat = flatten(t)
return max(flat)
def flatten(L):
'''
Returns a flattened version of nested list L
'''
# base case: list with one element
if len(L) == 1:
if type(L[0]) == list or type(L[0]) == tuple:
result = flatten(L[0])
else:
result = L
elif type(L[0]) == list or type(L[0]) == tuple:
result = flatten(list(L[0])) + flatten(list(L[1:]))
else:
result = [L[0]] + flatten(list(L[1:]))
return result
print(flatten((5, (1,2), [[1],[9]])))
t = (5, (1,2), [[1],[9]])
print(max_val(t))