본문 바로가기

Codility

[Lesson 2][Arrays] CyclicRotation

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

# K번 배열 회전하기

def solution(A, K):
    if not A:
        return A

    for _ in range(K):
        A.insert(0, A.pop())

    return A


print(solution([3, 8, 9, 7, 6], 3))  # [9, 7, 6, 3, 8]
print(solution([0, 0, 0], 1))  # [0, 0, 0]
print(solution([1, 2, 3, 4], 4))  # [1, 2, 3, 4]

'Codility' 카테고리의 다른 글

[Lesson 6][Sorting] Distinct  (0) 2021.05.07
[Lesson 5][Prefix Sums] CountDiv  (0) 2021.05.07
[Lesson 4][Counting Elements] FrogRiverOne  (0) 2021.05.07
[Lesson 3][Time Complexity] FrogJmp  (0) 2021.05.07
[Lesson 1][Iterations] BinaryGap  (0) 2021.05.07