Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- Brightics
- 팀 분석
- 삼성 SDS Brigthics
- 혼공머신러닝딥러닝
- Brightics Studio
- Brigthics를 이용한 분석
- 혼공머신
- 데이터분석
- 삼성SDS Brightics
- Brigthics Studio
- 모델링
- 데이터 분석
- 개인 의료비 예측
- 영상제작기
- 삼성SDS Brigthics
- 캐글
- 노코드AI
- 포스코 청년
- Brightics를 이용한 분석
- 삼성SDS
- 브라이틱스 서포터즈
- 브라이틱스
- 직원 이직률
- 혼공학습단
- 추천시스템
- 직원 이직여부
- Brigthics
- 혼공
- 포스코 아카데미
- 삼성 SDS
Archives
- Today
- Total
데이터사이언스 기록기📚
[프로그래머스/Python] Level 2_주차 요금 계산(2022 KAKAO BLIND RECRUITMENT) 본문
Coding Test/프로그래머스(Python)
[프로그래머스/Python] Level 2_주차 요금 계산(2022 KAKAO BLIND RECRUITMENT)
syunze 2022. 10. 30. 16:50📌문제 유형
2022 KAKAO BLIND RECRUITMENT
📌문제
📌나의 문제풀이
- fees의 요소 각각의 변수에 저장
- 주차 번호대로 정렬
- {차 번호 : 시간} 저장할 dict 초기화
- 차랑별 시간 계산(if - elif - else문 설명)
- i가 리스트의 마지막이고 in_out이 'IN'일 때 : 'OUT'이 없으므로 '23:59'로 설정하고 계산
- i가 리스트의 마지막이고 in_out이 'OUT'일 때 : 이미 저장되어 있는 in_time과 현재 out_time을 지정하여 계산
- [i][2]가 'IN'이고 [i+1][2]도 'IN'일 때 : 다음 리스트도 'IN'인 경우, 'OUT'이 없는 것이므로 '23:59'로 설정하여 계산
- 주차요금 계산
from datetime import datetime
import math
def solution(fees, records):
answer = []
time_list = {}
in_out_check = {}
basic_time, basic_fee, plus_time, plus_fee = fees
# 주차 번호대로 정렬
records_sort = [list(map(str,record.split(' '))) for record in records]
records_sort.sort(key = lambda x:x[1])
# dict 차 번호대로 초기화
for time, car, in_out in records_sort:
if car not in time_list.keys():
time_list[car] = 0
in_out_check[car] = 0
# 차량별 시간 계산 후 dict에 저장
for i in range(len(records_sort)):
time, car, in_out = records_sort[i]
if i == len(records_sort) - 1 and records_sort[i][2] == 'IN':
in_time = time
out_time = '23:59'
all_time = str(datetime.strptime(out_time,"%H:%M") - datetime.strptime(in_time,"%H:%M")).split(':')
time_list[car] += int(all_time[0]) * 60 + int(all_time[1])
elif i == len(records_sort) - 1 and records_sort[i][2] == 'OUT':
out_time = time
all_time = str(datetime.strptime(out_time,"%H:%M") - datetime.strptime(in_time,"%H:%M")).split(':')
time_list[car] += int(all_time[0]) * 60 + int(all_time[1])
elif records_sort[i+1][2] == 'IN' and records_sort[i][2] == 'IN':
in_time = time
out_time = '23:59'
all_time = str(datetime.strptime(out_time,"%H:%M") - datetime.strptime(in_time,"%H:%M")).split(':')
time_list[car] += int(all_time[0]) * 60 + int(all_time[1])
else:
if in_out == 'IN':
in_time = time
else:
out_time = time
all_time = str(datetime.strptime(out_time,"%H:%M") - datetime.strptime(in_time,"%H:%M")).split(':')
time_list[car] += int(all_time[0]) * 60 + int(all_time[1])
# 주차 요금 계산
time_list = sorted(time_list.items()) # 주차 번호대로 정렬
for car_num, time in time_list:
fee = 0
time -= basic_time
if time < 0:
answer.append(basic_fee)
else:
fee += basic_fee
fee += math.ceil(time/plus_time) * plus_fee
answer.append(fee)
return answer
📌다른 사람의 문제풀이
import math
def solution(fees, records):
check = {}
for record in records:
time, number, status = record.split()
time = time.split(':')
time = int(time[0])*60 + int(time[1])
if number not in check:
check[number] = (0, time, status)
if status == 'IN':
check[number] = (check[number][0], time, status)
elif status == 'OUT':
total_time, in_time, _ = check[number]
total_time += time - in_time
check[number] = (total_time, time, status)
result = {}
for number in check.keys():
total_time, time, status = check[number]
if status == 'IN':
total_time += 1439 - time
fee = fees[1]
if total_time <= fees[0]:
result[number] = fee
else:
fee = fee + math.ceil((total_time - fees[0]) / fees[2]) * fees[-1]
result[number] = fee
return list(map(lambda x : x[1], sorted(result.items())))
📌리뷰
- 시간 차이 계산하는 방법 : datetime.steptime()
- 딕셔너리 정렬 : sorted(딕셔너리.items())
- 예외 상황 잘 생각하기
728x90
'Coding Test > 프로그래머스(Python)' 카테고리의 다른 글
[프로그래머스/Python] Level 2_스킬트리(Summer/Winter Coding(~2018)) (0) | 2022.11.04 |
---|---|
[프로그래머스/Python] Level 2_오픈채팅방(2019 KAKAO BLIND RECRUITMENT) (0) | 2022.10.30 |
[프로그래머스/Python] Level 2_[1차] 뉴스 클러스터링(2018 KAKAO BLIND RECRUITMENT) (0) | 2022.10.28 |
[프로그래머스/Python] Level 2_n^2 배열 자르기(월간 코드 챌린지 시즌3) (0) | 2022.10.17 |
[프로그래머스] Level 2_튜플(2019 카카오 개발자 겨울 인턴십) (0) | 2022.10.14 |
Comments