Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

rotate array python

def rotate(arr,n,k):
    # write your code here
    k = k % n
    k = n - 1 - k
    arr[:] =  arr[k+1:] + arr[0:k+1]
    #print(arr[0:k+1])
    return arr
    
def main():
    n=int(input())
    arr=[]
    for i in range(n):
        val=int(input())
        arr.append(val)
    k=int(input())
    arr = rotate(arr,n,k)
    for i in range(n):
        print(arr[i],end=" ")

main()
Comment

rotate array in python

# Python3 program to rotate an array by
# d elements
# Function to left rotate arr[] of size n by d*/
def leftRotate(arr, d, n):
    for i in range(d):
        leftRotatebyOne(arr, n)
 
# Function to left Rotate arr[] of size n by 1*/
def leftRotatebyOne(arr, n):
    temp = arr[0]
    for i in range(n-1):
        arr[i] = arr[i + 1]
    arr[n-1] = temp
         
 
# utility function to print an array */
def printArray(arr, size):
    for i in range(size):
        print ("% d"% arr[i], end =" ")
 
  
# Driver program to test above functions */
arr = [1, 2, 3, 4, 5, 6, 7]
leftRotate(arr, 2, 7)
printArray(arr, 7)
 
# This code is contributed by Shreyanshi Arun
Comment

PREVIOUS NEXT
Code Example
Python :: python csv reader 
Python :: python read zipfile 
Python :: default ordering django 
Python :: pandas print all columns 
Python :: how to open sound file in python 
Python :: plt.suptitle position 
Python :: find number of common element in two python array 
Python :: python unit testing machine learning 
Python :: create 2d list dictionary 
Python :: add text to the middle of the window tkinter 
Python :: install nltk in python 
Python :: FileExistsError: [Errno 17] File exists: 
Python :: request.body django 
Python :: remove first 2 rows in pandas 
Python :: how to make sun as first day in calendar python 
Python :: discord py color 
Python :: change case python 
Python :: python gzip file 
Python :: how to make a full pyramid in python 
Python :: taking string input from user in python with try except 
Python :: how to do an if input = python 
Python :: How to install XGBoost package in python 
Python :: pandas save one row 
Python :: connect with pyodbc with statement 
Python :: charcodeat python 
Python :: convert string in list format to list python 
Python :: paginate on APIView drf 
Python :: how to check if email exists in python 
Python :: dataframe fill none 
Python :: how to create a countdown timer using python 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =