2
My teacher started talking about recursion, and passed a few exercises, only I stalled on a.
As stated in the following statement, I must create a function that returns the highest value contained in a list.
My difficulty is: I’m not able to do this using recursiveness. My idea was to make like one Bubble Sort and then only display the last element, however, the following error is occurring:
Recursionerror: Maximum recursion Depth exceeded in comparison
I can’t think of any other way.
Recursively implement a function Max
returns the largest value stored in a vector V
, containing n
integer
global c
global c2
global temp
x = [5, 2, 8, 4, 6, 9, 0, 1]
c = 0
c2 = 1
temp = max(x)
def Max(x):
global c
global c2
if x[c] > x[c2]:
x[c], x[c2] = x[c2], x[c]
c += 1
c2 += 1
if x[len(x)-1] != temp:
Max(x)
return x
print(Max(x))
Your identation is not correct. This in Python is a syntax error, and makes it very difficult to track your ideas. I think you should have ided Scroll the line with the space bar to format the question - this is not necessary. Edit the question, paste the cmo code is in your editor, select the code snippet and press the button
{}
to format the code here, please.– jsbueno
I didn’t know that. Thank you :)
– fernanda