0

I want to start by saying thank you for the help first.

I am tackling the cyclic rotation problem where you have to shift the contents of an list/array to the right and effectively wrapping the elements around so for example:

For example, given

 A = [3, 8, 9, 7, 6]
 K = 3

the function should return [9, 7, 6, 3, 8]. Three rotations were made:

 [3, 8, 9, 7, 6] -> [6, 3, 8, 9, 7]
 [6, 3, 8, 9, 7] -> [7, 6, 3, 8, 9]
 [7, 6, 3, 8, 9] -> [9, 7, 6, 3, 8] 

my code is as follows:

def solution(A, K):
    new_list = []
    first_index = 0
    for i in range(0, K):
        for num in range(0, len(A) - 1):
            new_list.insert(0, A.pop())
            print('new list {}'.format(new_list))
            print('old list {}'.format(A))
            if len(new_list) == 3:
                new_list.insert(len(new_list), A.pop(first_index))
                print(new_list)

after 3 rotations I get the list as A = [8, 9, 7, 6, 3] so to me it appears to place the last element from A to the front of the new_list.

So any help or points in the right direction would be helpful thank you again.

user2152012
  • 161
  • 1
  • 4
  • 17

6 Answers6

2

You can simply do it with this code.

def solution(A, K):
    K = K % len(A)
    return A[-K:] + A[:-K]

You can check more ways to do this here. Efficient way to rotate a list in python

Lead Developer
  • 1,146
  • 10
  • 26
1

So I realised I only had to loop around K times and check for an empty list. And what I got in the end is:

def solution(A, K):
    for i in range(0, K): # will perform K iterations of the below code
        if A == []: # check if list is empty
            return A # return A if A is the empty list
        A.insert(0, A.pop()) # inserts at the first index of A the last element of A
    return A # will return the list A
user2152012
  • 161
  • 1
  • 4
  • 17
0

Another Solution using deque from the collections module. It has an in-built rotate function.

from collections import deque
def solution(A, K):
    m=deque(A)
    m.rotate(K)
    return list(m)
Talha Tayyab
  • 8,111
  • 25
  • 27
  • 44
0
def slice(A):
    B = []
    for i  in range(0,len(A)) :
        B.append(A[-1+i])
    return B 

def solution(A, K):
    for i in range (1,K+1):
        A = slice(A)
    return A
seyyah
  • 1
  • 1
  • 2
    Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Jan 22 '22 at 09:34
0
def solution(A,K):        
    for k in np.arange(K):
        B=[]
        for i in range(len(A)):
            B.append(A[i-1])
        A=B
    return B
  • 2
    See "[Explaining entirely code-based answers](https://meta.stackoverflow.com/q/392712/128421)". While this might be technically correct, it doesn't explain why it solves the problem or should be the selected answer. We should educate along with helping solve the problem. – the Tin Man Mar 22 '22 at 05:13
0

The solution I found seems more of a clean code I guess

def solution(A, K):

        n = len(A)`enter code here`
        K = K % n # Ensure K is within the range of array size 
        rotated_arr = A[K:] + A[:K] # Rotating from end to the starting
        return rotated_arr
    A = [1, 2, 3, 4]
    K = int(input())
    rotated_arr = solution(A, K)
    print(rotated_arr)