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
- 부등호
- 대소 비교
- 가이드
- 스택/큐
- 파이썬
- 힙
- boolean
- 함수화
- deque
- ZIP
- 함수
- 코딩테스트
- 중첩
- 탈출 조건
- 변수명
- 매직 넘버
- 커리어
- 프로그래머스
- 그리디
- min heap
- 2020 채용
- 중간 변수
- permutations
- John Sonmez
- 중첩문
- 조합
- 가독성
- 임시 변수
- enumerate
- max-heap
Archives
- Today
- Total
Better Code, Better Life
기능개발 - 프로그래머스 - 파이썬 풀이 본문
문제 풀이
작업은 하루가 지나면 다 같이 업데이트 되고
100 이상이 되더라도 앞의 작업이 끝나지 않으면 배포가 되지 않습니다.
- 첫 번째 작업이 완료될 때까지 작업을 진행합니다.
- complete_first_progress
- 첫 번째 작업이 완료되면 첫번째부터 100 이상인 작업들을 배포합니다.
- update_progress_info
- 배포된 작업의 수를 저장합니다.
- complete_group_num.append(len(finished_progresses))
- 작업이 모두 배포될 때까지 1~3을 반복합니다.
update_progress_info에서 왼쪽 element를 반복적으로 빼야하기 때문에 list보다 deque를 이용합니다.
deque 자료구조를 통해 첫 번째 원소를 추출해내는 작업을 수월하게 할 수 있습니다.
프린터 문제 해설 에서 deque 설명을 참조하세요!
클린 코드 작성법
- 중간 절차를 명시하기 위해 변수를 이용합니다.
# before
progress_info[index] = (progress_info[0] + progress_info[1], progress_info[1])
# after
progress, speed = info
updated_progress = progress + speed
progress_info[index] = (updated_progress, speed)
- 문제 해결을 위해 기능을 나누고, 한 가지 기능만을 수행하도록 함수를 만듭니다.
- 문제 풀이와 같이 반복해야할 기능을 생각해두고 각각을 함수화합니다.
- 대소 비교는 작은 수를 왼쪽에 둡니다.
- 수직선과 비슷합니다. 그래야 사고가 자연스럽습니다.
- 반복문의 탈출 조건 (break) 과 함수의 탈출 조건 (return) 을 되도록 하나만 씁니다.
- 반복문을 머리로 시뮬레이션 할 때, 탈출 조건이 일정해야 쉽습니다. (가독성)
- 다른 언어로 속도를 개선시키자 할 때, 더 쉬워집니다. 같은 이유로 특정 언어에서만 지원하는 문법은 되도록 안쓰는 것이 좋습니다.
해답 코드
from collections import deque
def update_progress(progress_info):
for index, info in enumerate(progress_info):
progress, speed = info
updated_progress = progress + speed
progress_info[index] = (updated_progress, speed)
return progress_info
def complete_first_progress(progress_info):
# Keep updating process until first_process is completed.
while True:
progress_info = update_progress(progress_info)
first_process = progress_info[0]
progress = first_process[0]
if 100 <= progress:
break
return progress_info
def update_progress_info(progress_info):
# progress_info is updated by popleft().
finished_progresses = []
while True:
first_progress_info = progress_info[0]
progress = first_progress_info[0]
if 100 <= progress:
finished_progress = progress_info.popleft()
finished_progresses.append(finished_progress)
if progree < 100 or (not progress_info):
break
return finished_progresses
def solution(progresses, speeds):
progress_info = deque(zip(progresses, speeds))
complete_group_num = []
while progress_info:
progress_info = complete_first_progress(progress_info)
finished_progresses = update_progress_info(progress_info)
complete_group_num.append(len(finished_progresses))
return complete_group_num
도움이 됐다면 공감버튼을 눌러주세요! 질문이 있다면 댓글 달아주세요!
코딩테스트 연습 - 기능개발 | 프로그래머스
프로그래머스 팀에서는 기능 개선 작업을 수행 중입니다. 각 기능은 진도가 100%일 때 서비스에 반영할 수 있습니다. 또, 각 기능의 개발속도는 모두 다르기 때문에 뒤에 있는 기능이 앞에 있는 기능보다 먼저 개발될 수 있고, 이때 뒤에 있는 기능은 앞에 있는 기능이 배포될 때 함께 배포됩니다. 먼저 배포되어야 하는 순서대로 작업의 진도가 적힌 정수 배열 progresses와 각 작업의 개발 속도가 적힌 정수 배열 speeds가 주어질 때 각 배포마다 몇
programmers.co.kr
'Coding Test > Programmers' 카테고리의 다른 글
더 맵게 - 프로그래머스 - 파이썬 풀이 (0) | 2019.08.05 |
---|---|
주식가격 - 프로그래머스 - 파이썬 풀이 (0) | 2019.08.03 |
다리를 지나는 트럭 - 프로그래머스 - 파이썬 풀이 (0) | 2019.07.30 |
쇠막대기 - 프로그래머스 - 파이썬 풀이 (0) | 2019.07.28 |
탑 - 프로그래머스 - 파이썬 풀이 (0) | 2019.07.26 |
Comments