문제 : 뒤에서 5등 뒤로
def solution(num_list):
answer = msort(num_list)
return answer[5:]
def msort(num_list):
llen = len(num_list)
if llen <= 1:
return num_list
pivot = llen // 2
left = msort(num_list[:pivot])
right = msort(num_list[pivot:])
res = []
while left and right:
if left[0] < right[0]:
temp = left.pop(0)
res.append(temp)
else:
temp = right.pop(0)
res.append(temp)
while left:
temp = left.pop(0)
res.append(temp)
while right:
temp = right.pop(0)
res.append(temp)
return res
머지 소트를 구현해 보았다.
pivot은 값이 아닌 index로 설정해야 함을 다시 한 번 기억하자..
pivot을 값으로 설정하는 바보같은 짓을 했다.
실수했을 때 빨리 디버깅하는 게 중요해보인다.
근데 사실 그냥 sorted() 메소드 사용하는 문제였음.
def solution(num_list):
answer = sorted(num_list)
return answer[5:]
아니면 sort() 사용해도 되는데, 이 경우 아래와 같이 적으면 안된다.
def solution(num_list):
answer = num_list.sort()
return answer[5:]
sort()는 해당 리스트를 정렬하고 null을 반환한다. 이렇게 작성하면 안됨
def solution(num_list):
num_list.sort()
answer = num_list[5:]
return answer
이렇게 작성해야 된다.
'프로그래머스 > Lv. 1' 카테고리의 다른 글
7. 가장 많이 받은 선물 (0) | 2024.07.11 |
---|---|
6. PCCP 기출 1. 붕대 감기 (0) | 2024.07.11 |
4. 문자열 정수의 합 (0) | 2024.07.11 |
3. 특수문자 출력 in python (0) | 2024.07.11 |
2) 대소문자 in python (0) | 2024.07.11 |