문제 (2022 카카오 블라인드 채용)
- 주차장 요금표가 [기본 시간(분), 기본 요금(원), 단위 시간(분), 단위 요금(원)] 주어짐.
- 입/출차 기록이 ["시각, 차량번호, 내역", ... ] 주어짐.
- 기본 시간 이하면 기본 요금 청구.
- 기본 시간 초과면 기본 요금 + 초과 요금(단위 시간 * 단위 요금) 청구.
- 차량별 누적 주차 시간을 계산하여 요금을 일괄 정산. (차량번호 오름차순 리턴)
# Example
fees = [180, 5000, 10, 600]
records = [
"05:34 5961 IN",
"06:00 0000 IN",
"06:34 0000 OUT",
"07:59 5961 OUT",
"07:59 0148 IN",
"18:59 0000 IN",
"19:09 0148 OUT",
"22:59 5961 IN",
"23:00 5961 OUT"
]
'''
차량 번호 / 누적 주차 시간 / 주차 요금
0000 / 34 + 300 / 14600
0148 / 670 / 34400
5961 / 145 + 1 / 5000
'''
result = [14600, 34400, 5000]
풀이
import math
def solution(fees, records):
flag_per_car = {}
time_per_car = {}
for record in records:
time, car_num, flag = record.split(' ')
h, m = time.split(':')
h, m = int(h), int(m)
time_stamp = h*60 + m # 주차 시간을 계산하기 위해서
if flag == 'IN':
flag_per_car[car_num] = time_stamp
else:
enter_time_stamp = flag_per_car.get(car_num)
parking_time = time_stamp - enter_time_stamp
time_per_car[car_num] = time_per_car.get(car_num, 0) + parking_time
del flag_per_car[car_num]
for car_num, enter_time_stamp in flag_per_car.items():
last_time_stamp = 23*60 + 59
enter_time_stamp = flag_per_car.get(car_num)
parking_time = last_time_stamp - enter_time_stamp
time_per_car[car_num] = time_per_car.get(car_num, 0) + parking_time
cost_per_car = []
base_time, base_fee, unit_time, unit_fee = fees
for car_num, parking_time in time_per_car.items():
extra_cost = 0
if parking_time > base_time:
extra_cost = math.ceil((parking_time - base_time) / unit_time) * unit_fee
total_cost = base_fee + extra_cost
cost_per_car.append((car_num, total_cost))
cost_per_car = sorted(cost_per_car)
return [cost[1] for cost in cost_per_car]
해설
주차 시간(parking_time)을 계산하기 위해서 시각을 time_stamp 변수에 int로 저장.
parking_time을 계산해서 time_per_car 딕셔너리에 key=car_num : value=parking_time로 저장.
이때 flag_per_car으로 주차 상황을 체크, parking_time 계산이 끝났을때 flag_per_car에 값이 남아있다면 출차하지 않았으므로 last_time(23:59)-마지막 입차 시간 계산 후 parking_time에 더해줌.
이제 parking_time을 다 구했으므로, 요금(total_cost)을 계산하여 cost_per_car 리스트에 (car_num, total_cost)추가.
차량번호를 기준으로 오름차순 정렬해 cost_per_car 리스트 반환.
ps. 문제는 풀었지만, 마지막 입차 시간이 남아있을 경우 계산하는 부분의 중복 코드가 심히 거슬려서 추후에 다시 풀고 수정할 것 같다.
728x90
'개발' 카테고리의 다른 글
pyinstaller로 exe format 실행파일 만들기 (1) | 2022.08.17 |
---|---|
Raspi 4B 모니터 없이 셋업 중 VNC Viewer 'Cannot currently show the dekstop' (1) | 2022.05.30 |
Pololu - Tic Stepper Moto Controller (스텝모터 컨트롤러) (0) | 2022.04.29 |
[프로그래머스]신고 결과 받기 - Python (0) | 2022.04.29 |
[내가만든]당번 정하기 (0) | 2022.04.26 |