Skip to content

Instantly share code, notes, and snippets.

@Pratik-Shukla-22
Created November 21, 2021 05:02
Show Gist options
  • Save Pratik-Shukla-22/ea1ed3a46129165eb103fbcbfd5a6205 to your computer and use it in GitHub Desktop.
Save Pratik-Shukla-22/ea1ed3a46129165eb103fbcbfd5a6205 to your computer and use it in GitHub Desktop.
#Python program to reverse an array with specific start and end values:
def reverse_array(arr,start,end):
#Print the original array:
print(f"The original array is {arr}\n\n")
#Get the length of the array
n = len(arr)
#Initialize a variable for iterations
i = 1
#Run the following code until start<end
while(start<end):
#The following code swaps start and end values
temp = arr[start]
arr[start] = arr[end]
arr[end] = temp
#Increase the value of start by 1
start = start+1
#Decrease the value of end by 1
end = end-1
#Print the result after each iterations
print(f"Array after {i} iterations is {arr}")
#Increase the value of variable i by 1
i = i+1
#Return the array
return arr
#Function call:
answer = reverse_array([5,10,15,20,25,30],0,3)
print(f"\n\nThe reversed array is {answer}")
###################### Output ######################
#The original array is [5, 10, 15, 20, 25, 30]
#Array after 1 iterations is [20, 10, 15, 5, 25, 30]
#Array after 2 iterations is [20, 15, 10, 5, 25, 30]
#The reversed array is [20, 15, 10, 5, 25, 30]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment