Python Array Rotation

前端 未结 7 1293
夕颜
夕颜 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:30

    I expect that when you pass a slice of a to your recursive call, you're not passing the same variable any more. Try passing a in its entirety and the upper / lower bounds of your slice as additional arguments to your function.

    For instance consider this function:

    def silly(z):
      z[0] = 2
    

    I just tried the following:

    >>> z = [9,9,9,9,9,9]
    >>> silly(z)
    >>> z
    [2, 9, 9, 9, 9, 9]
    >>> silly(z[3:])
    >>> z
    [2, 9, 9, 9, 9, 9]
    

    Where you can see the modification to the slice was not retained by the full array

    Out of curiosity, what outputs do you get & what outputs do you expect?

提交回复
热议问题