프로그래머스 - 주차 요금 계산

문제 링크 : https://programmers.co.kr/learn/courses/30/lessons/92341

import math
limit = 23*60 + 59


def time_to_minute(time):
times = list(map(int, time.split(':')))
return times[0] * 60 + times[1]


def calCost(base_time, base_fee, per_time, per_fee, interval):

if base_time > interval:
return base_fee

return base_fee + math.ceil((interval-base_time)/per_time)*per_fee


def solution(fees, records):
answer = []

fees = list(map(int, fees))
base_time = fees[0]
base_fee = fees[1]
per_time = fees[2]
per_fee = fees[3]

dict = {}
time_dict = {}
fee_dict = {}

for line in records:
words = line.split(' ')
time = time_to_minute(words[0])
car_number = words[1]
in_out = words[2]

if in_out == 'IN':
dict[car_number] = [time]
else:
dict[car_number].append(time)
in_out_times = dict[car_number]

if car_number in time_dict:
time_dict[car_number] += (in_out_times[1] - in_out_times[0])
else:
time_dict[car_number] = (in_out_times[1] - in_out_times[0])

for key, value in dict.items():
if len(value) == 1:
if key in time_dict:
time_dict[key] += (limit - value[0])
else:
time_dict[key] = (limit - value[0])

for key, value in time_dict.items():
fee = calCost(base_time, base_fee, per_time,
per_fee, value)

if key in fee_dict:
fee_dict[key] += fee
else:
fee_dict[key] = fee

soredDict = sorted(fee_dict.items())
for value in soredDict:
answer.append(value[1])

return answer
Share