mamikun mamikun - 1 year ago 44
Python Question

Write a recursive function called rev, which reverses the order of elements in an array

Im new in this coding platform.

So i have a work to do which is reversing the order of elementsin an array recursively.

So here is what i did:

A= [4,2,7,3,9,1]

def rev(A):
if (len(A)==1):
return A[0]
return A[-1],pri(A[:-1])
print (rev(A))


(1, (4, (2, (7, (3, 9)))))

so i dont understand what i did wrongly,and if you can do it i want to do the output with ; instead of () things.
I have to do the program in python 3.0 or later versions.

Thank you.

Answer Source

Do it as below:

A= [4,2,7,3,9,1]

def rev(A):
    if len(A)==1:
        return A[0]
    lst = rev(A[:-1])
    return str(A[-1]) + ';' + str(lst)

print rev(A)

Works as below:

>>> ================================ RESTART ================================