Algorithm(python)

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

mihee 2022. 10. 15. 11:39

https://school.programmers.co.kr/learn/courses/30/lessons/92341

 

프로그래머스

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

programmers.co.kr

구현

1) 차 번호별 시간 구하기(defaultdict)

2) 요금 측정 함수

3) sorted() 차 번호 정렬

from collections import defaultdict

def solution(fees, records):
    dic = defaultdict(list)
    default_time, default_fee, unit_time, unit_fee = fees
    
    def get_price(log, fees):
        if len(log) % 2:
            log.append(23*60 + 59)
        time = sum(log[i+1] - log[i] for i in range(0, len(log),2))
        
        return unit_fee * -(-max(0,(time - default_time)) // unit_time) + default_fee
        
    for r in records:
        time, num, _  = r.split()
        h,m = map(int,time.split(':'))
        dic[num].append(h*60+m)
    
    return [get_price(dic[i],fees)for i in sorted(dic)]