0

I can't figure out why the program is behaving in a peculiar way. The program is meant to rotate an array cyclically for 'K' times. When the list A is assigned to B in function rotate_array(), the program behaves in an incorrect way as shown in the output. Whereas when it is changed with the line B=[0]*len(A), the problem disappears. Could someone please help ?

def rotate_array(A):
    #B = [0]*len(A)  # This solves the problem
    B = A   # This seems to cause a problem
    print "In rotate_array", A
    for index, item in enumerate(A):
        print "index:item ={}:{}, length of A={}".format(index, item, len(A))
        if index == (len(A) - 1):
            B[0] = A[index]
        else:
            B[index + 1] = item
    print B
    return B


def solution(A, K):
    for index, item in enumerate(A):
        print "in fn soln: index:item ={}:{}, length of A={}".format(index, item, len(A))
    ctr = 0
    while ctr < K:
        A = rotate_array(A)
        ctr += 1
    return A

if __name__ == '__main__':
    A = [1,2,3,4]
    K = 1
    ret_A = solution(A, K)
    print ret_A

Output:

in fn soln: index:item =0:1, length of A=4
in fn soln: index:item =1:2, length of A=4
in fn soln: index:item =2:3, length of A=4
in fn soln: index:item =3:4, length of A=4
In rotate_array [1, 2, 3, 4]
index:item =0:1, length of A=4
index:item =1:1, length of A=4
index:item =2:1, length of A=4
index:item =3:1, length of A=4
[1, 1, 1, 1]
[1, 1, 1, 1]

When function rotate_array(A) is changed to have this line

B = [0]*len(A)  # This solves the problem

instead of

B = A

The output is now correct -

in fn soln: index:item =0:1, length of A=4
in fn soln: index:item =1:2, length of A=4
in fn soln: index:item =2:3, length of A=4
in fn soln: index:item =3:4, length of A=4
In rotate_array [1, 2, 3, 4]
index:item =0:1, length of A=4
index:item =1:2, length of A=4
index:item =2:3, length of A=4
index:item =3:4, length of A=4
[4, 1, 2, 3]
[4, 1, 2, 3]
2
  • 5
    B=A creates an alias (two references to the same list). If you want a (shallow) copy use B = A[:]. Commented May 8, 2018 at 20:14
  • Thanks John! Now i see what was happening. Commented May 8, 2018 at 20:18

1 Answer 1

1

I see that John answered your question. I dont know all the details of what you are doing, so you may have a reason for looping, but I wanted to suggest an alternative implementation that didn't require looping K times if all you want is the final answer:

def rotate_array(A,K):
    A_length = len(A)
    B = [None] * A_length
    for A_index in range(len(A)):
        B_index = (A_index + K) % A_length
        B[B_index] = A[A_index]
    return(B)

A = list(range(10))
print("A = ", A)
for K in range(10):
    B = rotate_array(A,K)
    print("K = ",K, "B = ", B)

A =  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
K =  0 B =  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
K =  1 B =  [9, 0, 1, 2, 3, 4, 5, 6, 7, 8]
K =  2 B =  [8, 9, 0, 1, 2, 3, 4, 5, 6, 7]
K =  3 B =  [7, 8, 9, 0, 1, 2, 3, 4, 5, 6]
K =  4 B =  [6, 7, 8, 9, 0, 1, 2, 3, 4, 5]
K =  5 B =  [5, 6, 7, 8, 9, 0, 1, 2, 3, 4]
K =  6 B =  [4, 5, 6, 7, 8, 9, 0, 1, 2, 3]
K =  7 B =  [3, 4, 5, 6, 7, 8, 9, 0, 1, 2]
K =  8 B =  [2, 3, 4, 5, 6, 7, 8, 9, 0, 1]
K =  9 B =  [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.