프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
코드
from collections import defaultdict
# 시간 계산
def calculate_time(in_time, out_time):
in_h = int(in_time[:2])
in_m = int(in_time[3:])
out_h = int(out_time[:2])
out_m = int(out_time[3:])
if in_m < out_m:
return (out_h - in_h) * 60 + (out_m - in_m)
else:
return (out_h - in_h - 1) * 60 + ((60 - in_m) + out_m)
# 요금 계산
def calculate_fee(total_minute, fees):
if total_minute <= fees[0]:
return fees[1]
else:
if (total_minute - fees[0]) % fees[2] == 0:
f = ((total_minute - fees[0]) // fees[2]) * fees[3]
else:
f = ((total_minute - fees[0]) // fees[2] + 1) * fees[3]
return fees[1] + f
def solution(fees, records):
in_record = {} # 입차내역 {차 번호 : 입차시각}
accumulate_time = defaultdict(int)
for r in records:
time, car_num, inout = r.split()
if inout == "IN": # 입차
in_record[car_num] = time
else: # 출차
accumulate_time[car_num] += calculate_time(in_record[car_num], time)
in_record.pop(car_num) # 출차한 차 입차내역에서 삭제
# 출차 내역이 없는 차 처리
if in_record:
for car_num, in_time in in_record.items():
accumulate_time[car_num] += calculate_time(in_time, "23:59")
return [calculate_fee(m, fees) for car_num, m in sorted(accumulate_time.items())]
records 배열을 순서대로 탐색하며
- 입차한 경우 : in_record란 딕셔너리에 {차 번호, 입차 시간} 형태로 추가한다.
- 출차한 경우 : acculate_time이란 딕셔너리에 {차 번호 : 누적 시간}을 저장한다. 그리고 in_record에서 입차 내역을 삭제한다.
records 배열을 모두 탐색한 뒤에 in_record에 원소가 남아있다면 그건 입차 내역만 있고 출차 내역은 없는 차이므로 문제의 조건대로 출차 시각을 "23:59"로 하여 누적 시간을 계산한다.
accumulate_time의 원소들을 키값인 차량 번호대로 정렬하고 밸류값에 저장된 누적 시간으로 요금을 계산하여 배열로 만들어 리턴해준다.
'문제풀이 > 기타' 카테고리의 다른 글
[Python/파이썬] 프로그래머스 올바른 괄호 (0) | 2022.10.18 |
---|---|
[Python/파이썬] Summer/Winter Coding(~2018) 점프와 순간 이동 (0) | 2022.10.14 |
[Python/파이썬] 프로그래머스 최솟값 만들기 (0) | 2022.10.13 |
[Python/파이썬] 프로그래머스 Summer/Winter Coding(~2018) 영어 끝말잇기 (0) | 2022.10.11 |
[Python/파이썬] 프로그래머스 JadenCase 문자열 만들기 (0) | 2022.10.10 |
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
코드
from collections import defaultdict # 시간 계산 def calculate_time(in_time, out_time): in_h = int(in_time[:2]) in_m = int(in_time[3:]) out_h = int(out_time[:2]) out_m = int(out_time[3:]) if in_m < out_m: return (out_h - in_h) * 60 + (out_m - in_m) else: return (out_h - in_h - 1) * 60 + ((60 - in_m) + out_m) # 요금 계산 def calculate_fee(total_minute, fees): if total_minute <= fees[0]: return fees[1] else: if (total_minute - fees[0]) % fees[2] == 0: f = ((total_minute - fees[0]) // fees[2]) * fees[3] else: f = ((total_minute - fees[0]) // fees[2] + 1) * fees[3] return fees[1] + f def solution(fees, records): in_record = {} # 입차내역 {차 번호 : 입차시각} accumulate_time = defaultdict(int) for r in records: time, car_num, inout = r.split() if inout == "IN": # 입차 in_record[car_num] = time else: # 출차 accumulate_time[car_num] += calculate_time(in_record[car_num], time) in_record.pop(car_num) # 출차한 차 입차내역에서 삭제 # 출차 내역이 없는 차 처리 if in_record: for car_num, in_time in in_record.items(): accumulate_time[car_num] += calculate_time(in_time, "23:59") return [calculate_fee(m, fees) for car_num, m in sorted(accumulate_time.items())]
records 배열을 순서대로 탐색하며
- 입차한 경우 : in_record란 딕셔너리에 {차 번호, 입차 시간} 형태로 추가한다.
- 출차한 경우 : acculate_time이란 딕셔너리에 {차 번호 : 누적 시간}을 저장한다. 그리고 in_record에서 입차 내역을 삭제한다.
records 배열을 모두 탐색한 뒤에 in_record에 원소가 남아있다면 그건 입차 내역만 있고 출차 내역은 없는 차이므로 문제의 조건대로 출차 시각을 "23:59"로 하여 누적 시간을 계산한다.
accumulate_time의 원소들을 키값인 차량 번호대로 정렬하고 밸류값에 저장된 누적 시간으로 요금을 계산하여 배열로 만들어 리턴해준다.
'문제풀이 > 기타' 카테고리의 다른 글
[Python/파이썬] 프로그래머스 올바른 괄호 (0) | 2022.10.18 |
---|---|
[Python/파이썬] Summer/Winter Coding(~2018) 점프와 순간 이동 (0) | 2022.10.14 |
[Python/파이썬] 프로그래머스 최솟값 만들기 (0) | 2022.10.13 |
[Python/파이썬] 프로그래머스 Summer/Winter Coding(~2018) 영어 끝말잇기 (0) | 2022.10.11 |
[Python/파이썬] 프로그래머스 JadenCase 문자열 만들기 (0) | 2022.10.10 |