[Coding Test] 주차 요금 계산

ehwnghks·2022년 7월 12일
0

Coding TEST 연습

목록 보기
3/5

프로그래머스
2022 KAKAO BLIND RECRUITMENT
주차 요금 계산

1. 문제


2. 생각의 흐름

  1. 차번호 별로 입출차의 요금을 계산하는 문제이므로 차번호 마다 객체를 만들어 입출차를 관리하려함
  2. 입출차 기록을 확인하고 OUT이면 OUT-IN 시간만큼 기본료 + 추가요금을 계산하여 차량번호의 요금을 축적함
    2.1 문제는 OUT 될때마다 계산을 총합하는 것이 아닌 총 주차기록을 토대로 계산해야함
    2.2 시간 단위의 입출차 기록을 계산하기 편하게 분단위 변환 함수, 요금구하는 함수 생성
  3. 차번호 객체에 총 주차시간, 입차시간을 기록하고 IN시간이 있지만 OUT 시간이 없으면 '23:59' 기준으로 주차시간 포함하여 주차요금 계산

3. 나의 풀이

import math
def calMin(intime,outtime):
    intime=intime.split(':')
    outtime=outtime.split(':')
    in_min=int(intime[0])*60+int(intime[1])
    out_min=int(outtime[0])*60+int(outtime[1])
    return out_min-in_min

def getFee(total_min,fixedTime,fixedFee,variTime,variFee):
    if total_min-fixedTime <= 0:
        fee=fixedFee
    else:
        fee=fixedFee+math.ceil((float(total_min-fixedTime)/float(variTime)))*variFee
    return fee

def solution(fees, records):
    answer = []
    r_table={}
    f_time,f_fee,v_time,v_fee=fees
    for l in records:
        record=l.split(' ')
        if r_table.get(record[1]) is None:
            r_table[record[1]]={}
            r_table[record[1]]['in']=''
            r_table[record[1]]['min']=0
            if record[2]=='IN':
                r_table[record[1]]['in']=record[0]
        else:
            if record[2] != 'IN':
                r_table[record[1]]['min']+=calMin(r_table[record[1]]['in'],record[0])
                r_table[record[1]]['in']=''
            else:
                r_table[record[1]]['in']=record[0]
                
    for i in sorted(r_table):
        if r_table[i]['in'] !='':
            r_table[i]['min']+=calMin(r_table[i]['in'],'23:59')
            r_table[i]['in']=''
        answer.append(int(getFee(r_table[i]['min'],f_time,f_fee,v_time,v_fee)))
            

    return answer

4. 느낀점

문제를 제대로 보자.
좀 더 코드를 단순화 해보자.

profile
반갑습니다.

0개의 댓글