[programmers] 주차요금계산(Python)

zioo·2022년 6월 2일
0

주차요금계산

📌 Code

import math
 
def time_fun(t, in_out):
    o = list(t.split(':'))
    t = int(o[0])* 60 + int(o[1])
    if in_out == 'IN':
        t = t*(-1)
    return t
 
def solution(fees, records):
    answer = []
    cars = []
    note = {}
    for i in range(len(records)):
        cars.append(list(records[i].split()))
        note[cars[i][1]]=cars[i][0] # key = 차량번호 / value = input 시간 
 
    note = sorted(note.items(), key=lambda item: item[0]) # 차량 번호 기준 오름차순 
    
    for i in note:
        time = 0
        count = 0
        for j in range(len(cars)):
            if cars[j][1] == i[0]:
                time += time_fun(cars[j][0], cars[j][2])
                count +=1
        if count%2: # 나가는 시간이 없을 때 
            time += time_fun('23:59', 'OUT')
        if time>fees[0]: # 초과 포함 요금 계산 
            fee = fees[1] + math.ceil((time - fees[0]) / fees[2]) * fees[3]
        else: # 기본 요금 계산 
            fee = fees[1]
        answer.append(fee)
    return answer

0개의 댓글