반응형
Notice
Recent Posts
Recent Comments
Link
동캄의 코딩도장
프로그래머스 level2 [주차 요금 계산] 파이썬 본문
반응형
https://programmers.co.kr/learn/courses/30/lessons/92341
코딩테스트 연습 - 주차 요금 계산
[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"] [14600, 34400, 5000]
programmers.co.kr
# 프로그래머스 주차 요금 계산
def solution(fees, records):
answer = []
book = {}
carSpend = {}
cars = []
stdTime, stdFee, overTime, overFee = map(int, fees)
for record in records:
time, num, action = map(str, record.split())
carSpend[num] = 0
for record in records:
time, num, action = map(str, record.split())
if action == 'OUT':
outHour, outMin = time.split(':')
outTime = int(outHour)*60+int(outMin)
spend = outTime-book[num]
carSpend[num] += spend
book[num] = -1
else:
inHour, inMin = time.split(':')
inTime = int(inHour)*60+int(inMin)
book[num] = inTime
if book:
outTime = 23*60+59
for num in book:
if book[num] != -1:
spend = outTime-book[num]
carSpend[num] += spend
book[num] = -1
for car in carSpend:
if carSpend[car] > stdTime:
if (carSpend[car]-stdTime) % overTime == 0:
bill = stdFee+((carSpend[car]-stdTime)//overTime)*overFee
else:
bill = stdFee + \
((carSpend[car]-stdTime)//overTime+1)*overFee
else:
bill = stdFee
cars.append([car, bill])
cars.sort()
for car in cars:
answer.append(car[1])
return answer
record를 하나씩 받아서 각 시간, 차 , 동작( IN/OUT)을 판별하여, IN인 경우에는 시간을 계산하여 book[num]에 들어온 시간을 계산한다. OUT인 경우에는 나간시간에서 들어온 시간을 빼서 carSpend[num]에 시간을 갱신해준다.
나가지 않은 차들이 있는 경우에는 시간을 23:59로 계산하여, 사용한 시간을 계산하여 carSpend[num]에 시간을 갱신해준다.
최종적으로 요금을 계산한다.
반응형
'코테 > 프로그래머스' 카테고리의 다른 글
프로그래머스 level3 [입국심사] 파이썬 (0) | 2022.04.07 |
---|---|
프로그래머스 level2 [양궁대회] 파이썬 (0) | 2022.02.09 |
프로그래머스 level2 [k진수에서 소수 개수 구하기] 파이썬 (0) | 2022.02.09 |
프로그래머스 level1 [신고결과받기] 파이썬 (0) | 2022.02.09 |
프로그래머스 level3 [단속카메라] 파이썬 (0) | 2022.01.11 |