세용용용용 2023. 7. 20. 20:15

코딩테스트 연습 - 주차 요금 계산 | 프로그래머스 스쿨 (programmers.co.kr)

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

나의 코드

import math
def solution(fees, records):
    answer = []
    total_time = {}
    jucha_time = {}
    for i in records:
        list_i = i.split(' ')
        if list_i[1] not in total_time:
            total_time[list_i[1]] = 0
        #print(list_i)
        #들어온거면 추차 딕셔너리에 올리기
        if 'IN' in list_i:
            jucha_time[list_i[1]] = int(list_i[0].split(':')[0])*60+int(list_i[0].split(':')[1])
        #나간거면 total_time 딕셔너리에 시간 증감시켜주기
        else:
            total_time[list_i[1]] += (int(list_i[0].split(':')[0])*60+int(list_i[0].split(':')[1])) - jucha_time[list_i[1]]
            del jucha_time[list_i[1]]
   
    #최종적으로 남아있는 차량은 23:59분 출차로 판정
    for i in jucha_time:
        total_time[i] += (23*60)+59 - jucha_time[i]

    #차량 번호순으로 정렬하기
    #print(jucha_time)
    total_time = sorted(total_time.items(), key=lambda x:x[0], reverse=False)
   
    #최종적으로 요금 정산하기
    for i in total_time:
        #기본시간 이하이면 기본요금
        if i[1] <= fees[0]:
            answer.append(fees[1])
        #초과시 추가요금 부여
        else:
            answer.append(fees[1] + math.ceil((i[1]-fees[0])/fees[2])*fees[3])
    #print(answer)

    return answer
solution([180, 5000, 10, 600],
         ["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"])