프로그래머스 - Lv.2 주차 요금 계산 (Python3)

김진주·2022년 8월 24일
0
post-thumbnail

본 게시글은 2022 Kakao Blind Recruitment 코딩테스트 문제를 프로그래머스 코딩테스트연습에서 풀어본 내용을 기반으로 합니다.
해설, 정답코드만 보려면 목차 4. 문제 풀이로 넘어가주세요.

1. 문제 설명

주차장의 요금표와 차량이 들어오고(입차) 나간(출차) 기록이 주어졌을 때, 차량별로 주차 요금을 계산하려고 합니다.

2. 입출력 예시

3. 문제 분석 및 이해

4. 문제 풀이

def solution(fees, records):
    answer = []    
    #시간 계산
    carstime = {}
    finalprice = {}
    for item in records : 
        eachcar = item.split(" ")
        eachcar[0] = eachcar[0].split(":")
        eachcar.append(int(eachcar[0][0]) * 60 + int(eachcar[0][1]))
        if eachcar[1] not in carstime : 
            carstime[eachcar[1]] = eachcar[3]
        else : 
            carstime[eachcar[1]] = eachcar[3] - carstime[eachcar[1]]
            try : finalprice[eachcar[1]] += carstime.pop(eachcar[1]) # 현재 주차 목록에서 삭제
            except : finalprice[eachcar[1]] = carstime.pop(eachcar[1])
    for item in carstime : 
        try : finalprice[item] += (24*60 - 1) - carstime[item]
        except : finalprice[item] = (24*60 - 1) - carstime[item]
    for key in sorted(finalprice) : 
        tmptime = finalprice[key]
        if tmptime > fees[0] : 
            tmpprice = fees[1] -((fees[0]-tmptime)//fees[2])*fees[3] #math import 없이 반올림
        else : 
            tmpprice = fees[1]
        answer.append(tmpprice)
    return answer
profile
화성 갈래요. 아니 진짜로.

0개의 댓글