Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 백준
- 머신러닝
- 코테
- Python
- 웹스크래핑
- 추천 시스템
- pytorch
- SGD
- 분산 시스템
- 데이터 엔지니어링
- 파이썬
- 딥러닝
- 코딩테스트
- Overfitting
- codingtest
- 협업 필터링
- 데이터
- TF-IDF
- 시각화
- 웹크롤링
- 추천시스템
- Cosine-similarity
- recommendation system
- wordcloud
- selenium
- 프로그래머스
- 알고리즘
- Tensor
- 부스트캠프
- coursera
Archives
- Today
- Total
개발자식
[Codility Lesson2] CyclicRotation 본문
2. CyclicRotation
Test results - Codility
An array A consisting of N integers is given. Rotation of the array means that each element is shifted right by one index, and the last element of the array is moved to the first place. For example, the rotation of array A = [3, 8, 9, 7, 6] is [6, 3, 8, 9,
app.codility.com
나의 풀이
- 배열을 복사하여 원본 배열에서 값을 가져와서 복사된 배열에 값을 바꾸고
- k만큼 이동하면 총 길이를 넘어가는 경우와 아닌 경우를 나누어서 계산
def solution(A, K):
# write your code in Python 3.6
temp = A.copy()
if K==0 or len(A)==0:
return A
if K > len(A):
K = K % len(A)
for i in range(len(A)):
if i+K < len(A):
temp[i+K] = A[i]
else:
temp[i+K-len(A)] = A[i]
return temp
* 주의 : K의 범위가 0부터이기 때문에 따로 처리하지 않으면 % 연산자 때문에 에러 발생
다른 풀이
1. 뒤에서 하나씩 꺼내서 앞에 넣기 (총 K번)
def solution(A, K):
if A :
for i in range(K):
a = A.pop()
A.insert(0, a)
return A
2. 인덱싱
def solution(A, K):
# write your code in Python 3.6
if not (A and K):
return A
K = K % len(A)
return A[-K:] + A[:-K]
- 여기서도 마찬가지로 0일때 주의
'Algorithm > Codility' 카테고리의 다른 글
[Codility Lesson3] Time Complexity_ TapeEquilibrium (0) | 2022.06.29 |
---|---|
[Codility Lesson3] PermMissingElem (0) | 2022.06.29 |
[Codility Lesson3] Time Complexity_FrogJmp (0) | 2022.06.29 |
[Codility lesson2] OddOccurrencesInArray (0) | 2022.06.23 |
[Codility Lesson1] Iteration (0) | 2022.06.23 |
Comments