Python | Reverse array to a given position

Python Methods and Functions | reverse array

Examples:

 Input: arr [] = {1, 2, 3, 4, 5, 6} k = 4 Output: arr [] = {4, 3, 2, 1, 5 , 6} 

This problem has an existing solution, please refer to

# Program for accessing the array
# before this position

 

def reverseArrayUptoK ( input , k):

 

# reverse list starting at position k-1

# and split the rest of the list after k

# concatenate both parts and print

# input [k-1 :: - 1] - & gt; start list creation

# from k-1 positional element to first

# element in reverse

  print ( input [k - 1 :: - 1 ] + input [k:])

 
# Driver program

if __ name__ = = "__ main__"  :

input = [ 1 , 2 , 3 , 4 , 5 , 6 ]

  k = 4

reverseArrayUptoK ( input , k)

Output:

 [4, 3, 2, 1, 5, 6] 




Python | Reverse array to a given position: StackOverflow Questions

Tutorials