Skip to main content
Fixed the title to match the problem
Link
coderodde
  • 32.1k
  • 15
  • 78
  • 205

Function that takes an array of integers and returns that array rotated to the right N timescomponents

Function that takes an array of integers and returns that array rotated byto the right N positionstimes

Example:
if \$N=2\$, given the input array \${[1, 2, 3, 4, 5, 6]}\$ the function should return \${[5, 6, 1, 2, 3, 4]}\$

Here is my code:

def copyDigit(list,index,item):

        """
                Copy the item to the indexed location in the given list
        """
        list[index] = item
        return list

def rotateList(noListToRorate,timesRotate):

        """
                Rotate List to right    
        """
        print "Function received {0},{1}".format(noListToRorate,timesRotate)
        for rotate in range(N):
            lastItemInList = noListToRorate[-1]
            for i in range(len(noListToRorate)-2,-1,-1):
                itemToShift = noListToRorate[i]
                noListToRorate = copyDigit(noListToRorate,i+1,itemToShift)
            noListToRorate[0] = lastItemInList
        print "Rotate once: {0}".format(noListToRorate)
        return noListToRorate


if __name__ == '__main__':

        """
           This program will rorate right the given list N no of times  
        """

        arrayList = [1,2,3,4,5,6]
        N = 2
        print "Rotate an array: " ,(arrayList), "No of times: ", N
        finalList = rotateList(arrayList,N)
        print "Rotated List: ", finalList
     

Example:
if \$N=2\$, given the input array \${[1, 2, 3, 4, 5, 6]}\$ the function should return \${[5, 6, 1, 2, 3, 4]}\$

def copyDigit(list,index,item):

        """
                Copy the item to the indexed location in the given list
        """
        list[index] = item
        return list

def rotateList(noListToRorate,timesRotate):

        """
                Rotate List to right    
        """
        print "Function received {0},{1}".format(noListToRorate,timesRotate)
        for rotate in range(N):
            lastItemInList = noListToRorate[-1]
            for i in range(len(noListToRorate)-2,-1,-1):
                itemToShift = noListToRorate[i]
                noListToRorate = copyDigit(noListToRorate,i+1,itemToShift)
            noListToRorate[0] = lastItemInList
        print "Rotate once: {0}".format(noListToRorate)
        return noListToRorate


if __name__ == '__main__':

        """
           This program will rorate right the given list N no of times  
        """

        arrayList = [1,2,3,4,5,6]
        N = 2
        print "Rotate an array: " ,(arrayList), "No of times: ", N
        finalList = rotateList(arrayList,N)
        print "Rotated List: ", finalList
     

Example:
if \$N=2\$, given the input array \${[1, 2, 3, 4, 5, 6]}\$ the function should return \${[5, 6, 1, 2, 3, 4]}\$

Here is my code:

def copyDigit(list,index,item):

        """
                Copy the item to the indexed location in the given list
        """
        list[index] = item
        return list

def rotateList(noListToRorate,timesRotate):

        """
                Rotate List to right    
        """
        print "Function received {0},{1}".format(noListToRorate,timesRotate)
        for rotate in range(N):
            lastItemInList = noListToRorate[-1]
            for i in range(len(noListToRorate)-2,-1,-1):
                itemToShift = noListToRorate[i]
                noListToRorate = copyDigit(noListToRorate,i+1,itemToShift)
            noListToRorate[0] = lastItemInList
        print "Rotate once: {0}".format(noListToRorate)
        return noListToRorate


if __name__ == '__main__':

        """
           This program will rorate right the given list N no of times  
        """

        arrayList = [1,2,3,4,5,6]
        N = 2
        print "Rotate an array: " ,(arrayList), "No of times: ", N
        finalList = rotateList(arrayList,N)
        print "Rotated List: ", finalList
     
formatted example properly
Source Link
Vogel612
  • 25.5k
  • 7
  • 59
  • 141
Loading
added 60 characters in body
Source Link
Loading
pressed Ctrl+K
Source Link
Vogel612
  • 25.5k
  • 7
  • 59
  • 141
Loading
Source Link
Loading