5

To find the rotations of a number I wrote a code like

def rotation(N):
    A=[]
    for i in range(len(N)):
        y=N.pop(0)
        N.append(y)
        A.append(N)
    return A
K=[1,9,7]
r=rotation(K)
print(r)

but it gives me an output like:

A=[[1, 9, 7], [1, 9, 7], [1, 9, 7]]

but it should be

A=[[1,9,7],[9,7,1],[7,1,9]]

and I didnt understand why this happens thanks

4 Answers 4

5

Use a simple list slicing:

def rotation(N):
    output = []
    for i in range(len(N)):
        output.append(N[i:] + N[:i])
    return output

K=[1,9,7]
r=rotation(K)
print(r)
# [[1, 9, 7], [9, 7, 1], [7, 1, 9]]
Sign up to request clarification or add additional context in comments.

3 Comments

Why is this behavior in the first place?
As in why did the original code posted by OP does not work?
2

Use collections.deque

You should use collections.deque for this task and use the in-place method deque.rotate designed specifically for this purpose.

Using a list for this task would require expensive copying operations, while deque is optimised for fast addition and removal of elements from the beginning and end of a queue. See TimeComplexity for more details.

from collections import deque

A = deque([1, 9, 7])

for i in range(len(A)):
    print(A)
    A.rotate()

deque([1, 9, 7])
deque([7, 1, 9])
deque([9, 7, 1])

Why your code does not work

The reason your code does not work is because you are modifying the same object rather than a copy. The following will work:

def rotation(N):
    A = []
    for i in range(len(N)):
        N = N[:]
        N.append(N.pop(0))
        A.append(N)
    return A

K = [1,9,7]
r = rotation(K)

print(r)

[[9, 7, 1], [7, 1, 9], [1, 9, 7]]

Further explanation

If you modify the same object, A will consist of 3 lists with each list pointing to the same object and will therefore be guaranteed to be identical. Remember each list is just a bunch of pointers. If each pointer points to one object, changing it 3 times means the final assignment will be used for all the sublists.

7 Comments

Care to explain why?
@Sid, Sure, I've added more details.
Why is modifying the same object not working though? It's an append operation, so should be fine, isn't it?
If you modify the same object, A will consist of 3 lists with each list pointing to the same object and will therefore be guaranteed to be identical. Remember each list is just a bunch of pointers. If each pointer points to one object, changing it 3 times means the final assignment will be used for all the sublists.
Oh, that makes sense. I completely missed the object-reference relationship. Thanks!
|
1
def rotation(N):
    return [N[i:] + N[:i] for i in range(len(N))]
K = [1, 9, 7]
print(rotation(K))

3 Comments

This is basically the answer of @Austin. You should add a different approach, not repeat, what has been already posted. Please read How do I write a good answer?
I understand. It is my fault. Didn't see his answer.
-1

I see what you want to do. First of all, here is an improvement: You can spare this variable "y", so like this:

N.append(N.pop(0))

And then, you must copy the list:

A.append(N[:])

so the entire code looks like:

def rotation(N):
A=[]
for i in range(len(N)):
    N.append(N.pop(0))
    A.append(N[:])
return A

K=[1,9,7] r=rotation(K) print(r)

That does work. This is because of how Python manages lists. Hope this helped.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.