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 :: correlation meaning 
Python :: Function in python with input method 
Python :: open file in os python 
Python :: openpyxl 
Python :: Generate hashed passwords for ansible 
Python :: python screeninfo 
Python :: pandas get tuples from dataframe 
Python :: series object has no attribute split 
Python :: use get method request html page python 
Python :: functions in python programming 
Python :: accessing a variable from outside the function in python 
Python :: run multiprocesses on flask 
Python :: python count elements in sublists 
Python :: python how to iterate through a list of lists 
Python :: how to check python to see if list length is even 
Python :: insert an element in list python 
Python :: return array of sorted objects 
Python :: identity matrix with numpy 
Python :: numpy index of first true value 
Python :: python how to make boxplots with jitter 
Python :: generate table python 
Python :: how to declare a lambda in python 
Python :: Accessing elements from a Python Nested Dictionary 
Python :: date and time in python 
Python :: urllib_errors 
Python :: python int to byte 
Python :: #remove leading and trailing spaces 
Python :: channels_redis 
Python :: checking length of sets in python 
Python :: how to pass multiple parameters by 1 arguments in python 
ADD CONTENT
Topic
Content
Source link
Name
1+7 =