4

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

Layla
  • 117
  • 2
  • 7

4 Answers4

4

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]]
Austin
  • 25,759
  • 4
  • 25
  • 48
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.

jpp
  • 159,742
  • 34
  • 281
  • 339
  • 1
    Care to explain why? – Sid May 28 '18 at 16:36
  • 1
    @Sid, Sure, I've added more details. – jpp May 28 '18 at 16:41
  • Why is modifying the same object not working though? It's an append operation, so should be fine, isn't it? – Sid May 28 '18 at 16:42
  • 2
    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. – jpp May 28 '18 at 16:43
  • Oh, that makes sense. I completely missed the object-reference relationship. Thanks! – Sid May 28 '18 at 16:44
  • I didnt understandd the usage of N=N[:] what s the purpose of it ? Other then that its great to use it. I tried deque but it writes as deque in front of the each list and its not useful for me for further operations. – Layla May 28 '18 at 16:46
  • @user37487, `N[:]` is a **copy** of `N`. It's a *different* object, even if its contents are the same initially. You should read [these](https://stackoverflow.com/questions/9696495/python-when-is-a-variable-passed-by-reference-and-when-by-value) | [posts](https://stackoverflow.com/questions/986006/how-do-i-pass-a-variable-by-reference) for detailed explanations. – jpp May 28 '18 at 16:49
1
def rotation(N):
    return [N[i:] + N[:i] for i in range(len(N))]
K = [1, 9, 7]
print(rotation(K))
  • 1
    This is basically the [answer of @Austin](https://stackoverflow.com/a/50570547/8881141). You should add a different approach, not repeat, what has been already posted. Please read [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) – Mr. T May 28 '18 at 19:58
  • 1
    I understand. It is my fault. Didn't see his answer. – George Lutsenko May 28 '18 at 20:00
  • 1
    No problem. [There are so many questions unanswered...](https://stackoverflow.com/unanswered) – Mr. T May 28 '18 at 20:02
-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.

AGN Gazer
  • 8,025
  • 2
  • 27
  • 45
Grimmauld
  • 312
  • 2
  • 9