https://app.codility.com/programmers/lessons/2-arrays/cyclic_rotation/

 

CyclicRotation coding task - Learn to Code - Codility

Rotate an array to the right by a given number of steps.

app.codility.com

 

 

  • N개의 정수로 이루어진 배열 A가 주어질 때
  • 배열을 오른쪽으로 K번 이동한 결과 출력
  • 맨 마지막 요소는 맨 첫 번째 위치로 이동한다.
  • ex. A = [3,8,9,7,6] -> [6,3,8,9,7] -> [7,6,3,8,9]

 

def solution(A, K):
    if A:
        for _ in range(K):
            tmp = A.pop()
            A.insert(0, tmp)
    return A

# 다른 풀이
def solution(A, K):
    if not (A and K):
        return A

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

+ Recent posts