0
def solution(A):
    A_inp = A
    roated_a =list(range(len(A_inp)))
    
    k=1
    while k < 3:
        for i in range(len(A_inp)-1):
            
            roated_a[i+1] = A_inp[i]
            roated_a[0] = A_inp[len(A_inp)-1]
            
        A_inp = roated_a
        k+=1
        print(roated_a)
        

inp_arr = [int(item) for item in input("Enter the list items : ").split()]
solution(inp_arr)

This code should rotate the input array. Input given : 3 6 8 7 Expected Output should be : 7 3 6 8 ; 8 7 3 6; 6 8 7 3 But I am getting : 7 3 6 8 ; 7 7 7 7 ; 7 7 7 7

How is it getting the 7 for all the index after first iteration ? Where is the error ? Can anyone help ?

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
DEBASHIS BAIDYA
  • 310
  • 1
  • 4
  • 14
  • 1
    Does this answer your question? [Efficient way to rotate a list in python](https://stackoverflow.com/questions/2150108/efficient-way-to-rotate-a-list-in-python) – Marco Bonelli Jun 24 '22 at 17:40
  • No, I need to know what's wrong with this code. – DEBASHIS BAIDYA Jun 24 '22 at 17:41
  • Can you `elaborate` the function's requirements? (how it supposed to work) We can `guess` but rather not... – Daniel Hao Jun 24 '22 at 17:48
  • 1
    The code won't work as intended in Python 2. You can't use Python 2's `input()` function like that. It would have to be `raw_input()`. Please remove the [python-2.7] tag. – BoarGules Jun 24 '22 at 17:54

2 Answers2

2

A_inp = roated_a makes both variable names to refer the same object. So, when you update roated_a, you also change A_inp, starting 2nd iteration.

The fix (if you decide to stick with this implementation) is to make an actual copy:

A_inp = roated_a.copy()
Marat
  • 15,215
  • 2
  • 39
  • 48
  • How you think this method is ? I asking for your feedback to understand the solution thinking quality. – DEBASHIS BAIDYA Jun 24 '22 at 17:55
  • 2
    @DEBASHISBAIDYA honestly, it's rather complicated and I'd write something similar to Cardstdani's answer. Still, it was a useful example to learn this subtlety of Python – Marat Jun 24 '22 at 17:57
  • 1
    @DEBASHISBAIDYA- suggest for next post, try to explain your `logic` or requirements more clear - this way will get answer quicker. – Daniel Hao Jun 24 '22 at 17:58
  • Thank you very much guys for your feedback. Actually I am new and trying to learn without any help. I tried to solve the problem without using any builtin function. Your feedback will help me to understand more. – DEBASHIS BAIDYA Jun 24 '22 at 18:00
  • Disagreed. Python is, famously, a "batteries included" language; you have to learn the standard `built-in` and libs to make the best of this language. Right? – Daniel Hao Jun 24 '22 at 18:03
  • Yeah. But I am focusing more to thinking and algorithmic way, not only the language features. :) That's why... – DEBASHIS BAIDYA Jun 24 '22 at 18:10
0

You can rotate the list as follows to get the correct output:

def solution(A):
    for i in range(1, 4):
        print(A[i:] + A[:i])

inp_arr = [3, 6, 8, 7]
solution(inp_arr)

Output:

[6, 8, 7, 3]
[8, 7, 3, 6]
[7, 3, 6, 8]
Cardstdani
  • 4,999
  • 3
  • 12
  • 31