Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def r_max (given_list):
- largest = given_list[0]
- while type(largest) == type([]):
- largest = largest[0]
- for element in given_list:
- if type(element) == type([]):
- max_of_elem = r_max(element)
- if largest < max_of_elem:
- largest = max_of_elem
- else: # element is not a list
- if largest < element:
- largest = element
- return largest
- if (len(given_list) == 0)
- return None
- def r_max (given_list):
- largest = given_list[0]
- while type(largest) == type([]):
- largest = largest[0]
- for element in given_list:
- if type(element) == type([]):
- # If the list is empty, skip
- if(len(elemnt) == 0)
- next
- max_of_elem = r_max(element)
- if largest < max_of_elem:
- largest = max_of_elem
- else: # element is not a list
- if largest < element:
- largest = element
- return larges
- def items(x):
- if isinstance(x, list):
- for it in x:
- for y in items(it): yield y
- else: yield x
- for it in x:
- for y in items(x): yield y
- for it in x: yield from it
- def r_max(lst):
- new_lst = []
- for i in lst:
- try:
- new_lst.extend(i)
- except TypeError:
- new_lst + [i]
- return max(new_lst)
- A = [2, 4, 6, 8, [[11, 585, "tu"], 100, [9, 7]], 5, 3, "ccc", 1]
- def M(L):
- # If list is empty, return nothing
- if len(L) == 0:
- return
- # If the list size is one, it could be one element or a list
- if len(L) == 1:
- # If it's a list, get the maximum out of it
- if isinstance(L[0], list):
- return M(L[0])
- # If it's a string, ignore it
- if isinstance(L[0], str):
- return
- # Else return the value
- else:
- return L[0]
- # If the list has more elements, find the maximum
- else:
- return max(M(L[:1]), M(L[1:]))
- print A
- print M(A)
- def find_max(L):
- if len(L) == 0:
- return float("-inf")
- elif len(L) == 1:
- if isinstance(L[0], list):
- return find_max(L[0])
- else:
- return L[0]
- elif len(L) == 2:
- if isinstance(L[0], list):
- firstMax = find_max(L[0])
- else:
- firstMax = L[0]
- if isinstance(L[1], list):
- lastMax = find_max(L[1])
- else:
- lastMax = L[1]
- if firstMax > lastMax:
- return firstMax
- else:
- return lastMax
- else:
- if isinstance(L[0], list):
- firstMax = find_max(L[0])
- lastMax = find_max(L[1:])
- if firstMax > lastMax:
- return firstMax
- else:
- return lastMax
- else:
- lastMax = find_max(L[1:])
- if L[0] > lastMax:
- return L[0]
- else:
- return lastMax
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement