1

The following function for some reason is displaying different values of q1 at two different executions of it. I use Pycharm and the value of the last element in q1 is different in both the outputs it is displaying

def rotation(mat):
    q1 = mat[0]
    q2 = [mat[i][-1] for i in range(len(mat))]
    print('\n\n', q1, q2)

    ####### This is where it goes wrong
    print('Actual:', q1[len(q1)-1], len(q1)-1)
    for i in range(0, len(q1)):
        mat[i][-1] = q1[i]
        print('Obtained:', q1[i], i)
    ##########################


    # display(mat)
    q1 = q2
    q2 = mat[-1][::-1]

    for i in range(len(mat[-1]), 0):
        mat[-1][i] = q1[len(mat)-i-1]

    # display(mat)
    q1 = q2
    q2 = [mat[i][0] for i in range(len(mat)-1, 0, -1)]

    for i in range(len(mat)-1, 0, -1):
        mat[i][0] = q1[len(mat)-i-1]

    # display(mat)
    q1 = q2

    for i in range(len(mat[0])-1 ):
        mat[0][i] = q1[i]

    print('\n\n')
    # display(mat)

UPDATE: The value is printing correctly if I comment out the line where I assign the value to mat[i][-1].

  • I can't point out the exact problem but I think the basic cause of it is explained [here](https://stackoverflow.com/questions/29785084/changing-one-list-unexpectedly-changes-another-too). – Autonomous Oct 04 '18 at 00:01

0 Answers0