

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python program to reverse an array up to a given position
Here given user input array and the position of the array to be reverse.so our task is to reverse this array. We just reverse the sub array from [0 to (n-1)].
Example
Input : A=[3, 7, 6, 4, 1, 2] n=4 Output : [1, 4, 6, 7, 3, 2]
Algorithm
Step 1: reverse list starting from n-1 position. Step 2: split remaining list after n. Step 3: concat both parts and prints
Example code
#Program to reverse an array #up to a n position def arrayreverse(A, n): #generate list starting from n-1 position element till first element in #reverse order print ("REVERSE OF AN ARRAY UPTO",n,"POSITION",A[n-1::-1] + A[n:]) # Driver program if __name__ == "__main__": A=list() n1=int(input("Enter the size of the List ::")) print("Enter the Element of List ::") for i in range(int(n1)): k=int(input("")) A.append(k) n=int(input("Enter the position to be reverse ::")) arrayreverse(A, n)
Output
Enter the size of the List :: 6 Enter the Element of List :: 2 3 4 1 78 23 Enter the position to be reverse : 5 REVERSE OF AN ARRAY UPTO 5 POSITION [78, 1, 4, 3, 2, 23]
- Related Questions & Answers
- Program to reverse an array up to a given position in Python
- Java program to reverse an array upto a given position
- Python program to reverse an array in groups of given size?
- Program to reverse the position of each word of a given string in Python
- Python program to reverse a Numpy array?
- Java program to reverse an array
- C# program to reverse an array
- Write a Golang program to reverse an array
- Java program to reverse an array in groups of given size
- Java Program to reverse a given String with preserving the position of space.
- Python program to sort and reverse a given list
- C program to reverse an array elements
- Write a program to reverse an array in JavaScript?
- Golang Program to reverse a given linked list.
- Write a C program to reverse array
Advertisements