0

I am attempting to write a reverse function by recursive method, somehow it returns none. Can anyone help? Thanks a lot.

def reverse(S, start, stop):

    if start>=stop:
        return False

    elif start<stop-1:
        S[start], S[stop-1]=S[stop-1], S[start]
        reverse(S, start+1, stop-1)
# test the code       
myList=[2, 3, 4, 5, 51, 66, 88, 234, 444]
print "input sequence is : "+str(myList)
print reverse(myList, 0, len(myList))
  • The `reverse()` function alters the list **in place**, and returns either `None` or `False`. It never will return the reversed list. – Martijn Pieters Feb 06 '15 at 15:06
  • 1
    @jonrsharpe: not the case here, but the post is a duplicate anyway; the code here appears to be a version of [this approach](http://stackoverflow.com/a/217204), which reverses recursively **in place**. – Martijn Pieters Feb 06 '15 at 15:08
  • Remove the `print` from the `reverse(..)` call, and add `print myList` at the end. – Martijn Pieters Feb 06 '15 at 15:10

0 Answers0