mamikun - 5 months ago 7x

Python Question

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]

else:

return A[-1],pri(A[:-1])

print (rev(A))

Output:

(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

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 ================================
>>>
1;9;3;7;2;4
>>>
```

Source (Stackoverflow)

Comments