Last active
November 21, 2021 04:58
-
-
Save Pratik-Shukla-22/b5e299221c5586b7f5a7632514b6f02e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Python program to reverse an array: | |
def reverse_array(arr): | |
#Print the original array: | |
print(f"The original array is {arr}\n\n") | |
#Get the length of the array | |
n = len(arr) | |
#Define the start and end points | |
start = 0 | |
end = n-1 | |
#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]) | |
print(f"\n\nThe reversed array is {answer}") | |
###################### Output ###################### | |
#The original array is [5, 10, 15, 20, 25, 30] | |
#Array after 1 iterations is [30, 10, 15, 20, 25, 5] | |
#Array after 2 iterations is [30, 25, 15, 20, 10, 5] | |
#Array after 3 iterations is [30, 25, 20, 15, 10, 5] | |
#The reversed array is [30, 25, 20, 15, 10, 5] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment