[프로그래머스 Lv2.] 주차 요금 계산(python)

gayoung·2022년 3월 12일
0

알고리즘

목록 보기
3/50

1. 문제

문제 설명

제한사항

입출력 예시

입출력 예 설명


2. 풀이 과정

내가 생각한 진행 과정

  • 기록들을 확인하면서 in인 경우 차번호+해당시간(in_time) 저장, out인 경우 차번호 이용해 주차시간 계산(out_time - in_time)
  • check = {차번호: in_time}
    • check = {'5961': '0', '0000': '18:59', '0148': '0'}
    • 같은 차번호에 중복으로 in이 온다면 그냥 엎기 -> 이미 out된 차가 들어오는것이기 때문에 영향x
    • 차 out하면 check = {차번호 : 0}으로 해주기! -> 마지막 시각(23:59)에 출차되는 경우 계산하기 위해
  • check_time = {차번호 : 주차시간계산}
    • check_time = {'0000': 34, '5961': 146, '0148': 670}
    • 주차시간은 changeToMinute함수 만들기 -> 시간을 분으로 환산해서 주차시간 계산
  • 23:59분에 출차되는 경우 주차시간계산
  • 차량 번호가 작은 자동차부터 청구되기 때문에 sort진행
    • check_time = [[차번호, 주차시간], [차번호, 주차시간], [차번호, 주차시간]]
  • 이를 이용해 주차요금 구하기

최종 코드

import math

def changeToMinute(first, second):
    h1, m1 = map(int, first.split(':'))
    h2, m2 = map(int, second.split(':'))
    total1, total2 = h1 * 60 + m1, h2 * 60 + m2

    return total2 - total1;

def solution(fees, records):
    dt, df, ut, uf = fees
    check = {}
    check_time = {}
    
    # in-out 둘다 있는 차량 주차시간 구하기
    for record in records:
        when, car, inout = record.split()
        if inout == "IN":
            check[car] = when
        else:
            if car not in check_time:
                check_time[car] = changeToMinute(check[car], when)
            else:
                check_time[car] += changeToMinute(check[car], when)
            check[car] = "0"
            
    # 23:59에 출차된 것으로 간주할 때 주차시간 구하기
    for key, value in check.items():
        if value != "0":
            if key in check_time:
                check_time[key] += changeToMinute(value, "23:59")
            else:
                check_time[key] = changeToMinute(value, "23:59")  # 테스트3번에서 check_time에 아예 아무것도 안들어가있을 수도 있음

    check_time = sorted(check_time.items())

    answer = []
    for car, total_time in check_time:
        if total_time <= dt:
            answer.append(df)
        else:
            answer.append(df + math.ceil((total_time - dt) / ut) * uf)

    return answer

발생했던 문제

1. AttributeError: 'function' object has no attribute 'time'

  • changeToMinute 함수명을 time으로 설정했을 때, 발생
  • 예약어 또는 이미 사용된 time을 함수명으로 사용했기 때문

2. keyerror: '1234'

  • check_time이 빈 딕셔너리인 경우 23:59에 출차된 것으로 간주할 때 주차시간 구하기(
  • 예시3번에서 보면, check = {'1234': '00:00'}, check_time = {}이므로 차번호 1234에 대한 기존 주차시간이 없음

0개의 댓글

관련 채용 정보