기타/Codility

[Easy] CyclicRotation

백곰곰 2022. 5. 22. 16:21
728x90
반응형

[문제]

주어진 수(K)만큼 1차원 배열을 회전하는 문제

 

[코드]

# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")

def solution(A, K):
    # write your code in Python 3.6
    lenth = len(A)

    if lenth==0 or K==0:
        return A
    
    K%=lenth
    answer=[0 for i in range(lenth)]
  
    for i in range(0, lenth) :
        answer[(i+K)%lenth] = A[i]
    
    return answer

* 0에 대한 별도 처리 필요

 

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

 

 

728x90

'기타 > Codility' 카테고리의 다른 글

[Easy] Distinct  (0) 2022.05.25
[Easy] PassingCars  (0) 2022.05.24
[Easy] FrogJmp  (0) 2022.05.22
[Easy] ArrListLen  (0) 2022.05.22
[Easy] BinaryGap  (0) 2022.05.22