Python Array Rotation

前端 未结 7 1257
夕颜
夕颜 2020-12-03 08:24

So I am implementing a block swap algorithm in python.

The algorithm I am following is this:

Initialize A = arr[0..d-1] and B = arr[d..n-1] 1) Do following u

相关标签:
7条回答
  • 2020-12-03 08:47

    you can use this code for left rotation in python array

    import copy
    def leftRotate(arr,n) :
        _arr = copy.deepcopy(arr)
        for i in range(len(arr)-n):
            arr[i] = arr[i+n]
        for j in range(n):
            arr[(len(arr)-n)+j] = _arr[j]
    
    arr = [1, 2, 3, 4, 5, 6, 7] 
    leftRotateby = 3
    leftRotate(arr,leftRotateby)
    print arr 
    #output:: [4, 5, 6, 7, 1, 2, 3]
    
    0 讨论(0)
提交回复
热议问题