[Programmers] Level 2. 주차 요금 계산

Seo Seung Woo·2022년 9월 19일
0
post-thumbnail

Level 2. 주차 요금 계산


❔Thinking

  • 입차 시간과 출차 시간을 활용해 주차 요금을 계산한다.
  • 출차시간이 없을 경우 23:59출차로 계산한다.
  • 주차요금은 올림을 하여 계산한다.

💻Solution

from collections import defaultdict

def ParkingTime(time_list:list)->int:
    parking_time = 0
    if len(time_list) % 2 != 0:
        time_list.append('23:59')
    for i in range(1,len(time_list),2):
        IN = list(map(int, time_list[i-1].split(':')))
        OUT = list(map(int, time_list[i].split(':')))
        if OUT[1] < IN[1]:
            OUT[0], OUT[1] = OUT[0]-1, OUT[1]+60
        parking_time += (OUT[0]-IN[0])*60 + OUT[1]-IN[1]
    return parking_time

def HowMuchFee(fees, parking_time):
    fee = fees[1]
    if parking_time > fees[0]:
        if (parking_time - fees[0]) / fees[2] != int((parking_time - fees[0]) / fees[2]):
            fee += (int((parking_time - fees[0]) / fees[2]) + 1) * fees[3]
        else:
            fee += (int((parking_time - fees[0]) / fees[2])) * fees[3]
    return fee


def solution(fees, records):
    answer = {}
    InOutByCar = defaultdict(list)
    # 자동차별 입차/출차 확인
    for info in records:
        information = info.split()
        InOutByCar[information[1]].append(information[0])
    # 자동차별 요금 확인
    for k,v in InOutByCar.items():
        parking_time = ParkingTime(v)
        answer[k] = HowMuchFee(fees, parking_time)
    return [fee[1] for fee in sorted(answer.items())] 

🗝️keypoint

  1. dict의 정렬은 sorted로 하고, key로 정렬시 items, value로 정렬시 lambda를 활용한다.

    a = {'1': '라', '2':'다', '3':'나', '4':'가'}
    print(sorted(a.items()))
    print(sorted(a.items(), reverse=True))
    print(sorted(a.items(), key = lambda x: x[1]))
    print(sorted(a.items(), key = lambda x: x[1], reverse=True))
    # [('1', '라'), ('2', '다'), ('3', '나'), ('4', '가')]
    # [('4', '가'), ('3', '나'), ('2', '다'), ('1', '라')]
    # [('4', '가'), ('3', '나'), ('2', '다'), ('1', '라')]
    # [('1', '라'), ('2', '다'), ('3', '나'), ('4', '가')]
  2. 내림, 올림, 반올림은 math 라이브러리를 사용할 수 있다.
    math.floor(내림) / math.ceil(올림) / math.round(반올림)

profile
Code for people

0개의 댓글