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]
'코딩테스트 > 인프런' 카테고리의 다른 글
[Python] 4장 이분탐색(결정 알고리즘)&그리디 내용 정리 (0) | 2022.08.02 |
---|---|
[Python] 3장 string&탐색&시뮬레이션 내용 정리 (0) | 2022.08.02 |
[codility] Lesson 1. Iterations - BinaryGap (0) | 2022.08.01 |
[Python] 2장 구현 내용 정리 - 약수, 소수, 반올림 (0) | 2022.08.01 |
[Python] 1장 파이썬 코딩테스트 기초 문법 정리 (0) | 2022.07.21 |