-4

so I was trying to reverse an array by recursion so the code logic is we are swaping right most end value to the left most end. I have implemented logic but the thing is My recursion function retuns None when I explicity wants to return the reversed array. so I wanna know why it is returning None when I was trying to return an array. I have given a code below so please someone can explain me why it is happening and thank you in advance

def reverse_array(left,right,array):
    if left > right:
        return array
    swap = 0 
    swap = array[left]
    array[left] = array[right]
    array[right]=swap
    reverse_array(left+1,right-1,array)
    
    
array = [1,2,3,4,5]
print(reverse_array(0,4,array))
2
  • 2
    stackoverflow.com/a/17778390/3514144 Commented Dec 2 at 4:05
  • 1
    Function should return a function to be a recursive, right? Solution: - replace return reverse_array(left+1,right-1,array) Happy Coding! Commented Dec 2 at 8:22

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.