Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. def r_max (given_list):
  2. largest = given_list[0]
  3. while type(largest) == type([]):
  4. largest = largest[0]
  5.  
  6. for element in given_list:
  7. if type(element) == type([]):
  8. max_of_elem = r_max(element)
  9. if largest < max_of_elem:
  10. largest = max_of_elem
  11. else: # element is not a list
  12. if largest < element:
  13. largest = element
  14.  
  15. return largest
  16.  
  17. if (len(given_list) == 0)
  18. return None
  19.  
  20. def r_max (given_list):
  21. largest = given_list[0]
  22. while type(largest) == type([]):
  23. largest = largest[0]
  24.  
  25. for element in given_list:
  26. if type(element) == type([]):
  27. # If the list is empty, skip
  28. if(len(elemnt) == 0)
  29. next
  30. max_of_elem = r_max(element)
  31. if largest < max_of_elem:
  32. largest = max_of_elem
  33. else: # element is not a list
  34. if largest < element:
  35. largest = element
  36.  
  37. return larges
  38.  
  39. def items(x):
  40. if isinstance(x, list):
  41. for it in x:
  42. for y in items(it): yield y
  43. else: yield x
  44.  
  45. for it in x:
  46. for y in items(x): yield y
  47.  
  48. for it in x: yield from it
  49.  
  50. def r_max(lst):
  51. new_lst = []
  52. for i in lst:
  53. try:
  54. new_lst.extend(i)
  55. except TypeError:
  56. new_lst + [i]
  57. return max(new_lst)
  58.  
  59. A = [2, 4, 6, 8, [[11, 585, "tu"], 100, [9, 7]], 5, 3, "ccc", 1]
  60.  
  61. def M(L):
  62.  
  63. # If list is empty, return nothing
  64. if len(L) == 0:
  65. return
  66.  
  67. # If the list size is one, it could be one element or a list
  68. if len(L) == 1:
  69.  
  70. # If it's a list, get the maximum out of it
  71. if isinstance(L[0], list):
  72. return M(L[0])
  73. # If it's a string, ignore it
  74. if isinstance(L[0], str):
  75. return
  76. # Else return the value
  77. else:
  78. return L[0]
  79.  
  80. # If the list has more elements, find the maximum
  81. else:
  82. return max(M(L[:1]), M(L[1:]))
  83.  
  84.  
  85. print A
  86. print M(A)
  87.  
  88. def find_max(L):
  89.  
  90. if len(L) == 0:
  91. return float("-inf")
  92. elif len(L) == 1:
  93. if isinstance(L[0], list):
  94. return find_max(L[0])
  95. else:
  96. return L[0]
  97. elif len(L) == 2:
  98. if isinstance(L[0], list):
  99. firstMax = find_max(L[0])
  100. else:
  101. firstMax = L[0]
  102. if isinstance(L[1], list):
  103. lastMax = find_max(L[1])
  104. else:
  105. lastMax = L[1]
  106. if firstMax > lastMax:
  107. return firstMax
  108. else:
  109. return lastMax
  110. else:
  111. if isinstance(L[0], list):
  112. firstMax = find_max(L[0])
  113. lastMax = find_max(L[1:])
  114. if firstMax > lastMax:
  115. return firstMax
  116. else:
  117. return lastMax
  118. else:
  119. lastMax = find_max(L[1:])
  120. if L[0] > lastMax:
  121. return L[0]
  122. else:
  123. return lastMax
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement