์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
- Lambda
- ๋ฐ์ดํฐ๋ถ์
- LDA
- numpy
- ์๋ํด๋ผ์ฐ๋
- ๋ฐ์ดํฐ๋ถ์์ ๋ฌธ๊ฐ
- pandas
- ์ค๋ฒ์ํ๋ง
- ADP
- ADsP
- ๋ ๋ฆฝํ๋ณธ
- datascience
- ๊ตฐ์งํ
- iloc
- Python
- ๋น ๋ฐ์ดํฐ๋ถ์๊ธฐ์ฌ
- ํฌ๋กค๋ง
- opencv
- PCA
- ๋น ๋ฐ์ดํฐ
- ํ์ด์ฌ
- ํ ์คํธ๋ถ์
- ๋์ํ๋ณธ
- DBSCAN
- dataframe
- ๋ฐ์ดํฐ๋ถ์์ค์ ๋ฌธ๊ฐ
- ๋ฐ์ดํฐ๋ถ๊ท ํ
- t-test
- ์ธ๋์ํ๋ง
- ์ฃผ์ฑ๋ถ๋ถ์
Data Science LAB
[Python] ํ๋ก๊ทธ๋๋จธ์ค ์ฝ๋ฉํ ์คํธ ์ฐ์ต level 2 (์ฃผ์ฐจ ์๊ธ ๊ณ์ฐ) ๋ณธ๋ฌธ
[Python] ํ๋ก๊ทธ๋๋จธ์ค ์ฝ๋ฉํ ์คํธ ์ฐ์ต level 2 (์ฃผ์ฐจ ์๊ธ ๊ณ์ฐ)
ใ ใ ใ ใ 2022. 11. 5. 02:071. ๋ฌธ์ ์ค๋ช
์ฃผ์ฐจ์ฅ์ ์๊ธํ์ ์ฐจ๋์ด ๋ค์ด์ค๊ณ (์ ์ฐจ) ๋๊ฐ(์ถ์ฐจ) ๊ธฐ๋ก์ด ์ฃผ์ด์ก์ ๋, ์ฐจ๋๋ณ๋ก ์ฃผ์ฐจ ์๊ธ์ ๊ณ์ฐํ๋ ค๊ณ ํฉ๋๋ค. ์๋๋ ํ๋์ ์์๋ฅผ ๋ํ๋ ๋๋ค.
2. ์ ํ ์กฐ๊ฑด
- fees์ ๊ธธ์ด = 4
- fees[0] = ๊ธฐ๋ณธ ์๊ฐ(๋ถ)
- 1 ≤ fees[0] ≤ 1,439
- fees[1] = ๊ธฐ๋ณธ ์๊ธ(์)
- 0 ≤ fees[1] ≤ 100,000
- fees[2] = ๋จ์ ์๊ฐ(๋ถ)
- 1 ≤ fees[2] ≤ 1,439
- fees[3] = ๋จ์ ์๊ธ(์)
- 1 ≤ fees[3] ≤ 10,000
- 1 ≤ records์ ๊ธธ์ด ≤ 1,000
- records์ ๊ฐ ์์๋ "์๊ฐ ์ฐจ๋๋ฒํธ ๋ด์ญ" ํ์์ ๋ฌธ์์ด์ ๋๋ค.
- ์๊ฐ, ์ฐจ๋๋ฒํธ, ๋ด์ญ์ ํ๋์ ๊ณต๋ฐฑ์ผ๋ก ๊ตฌ๋ถ๋์ด ์์ต๋๋ค.
- ์๊ฐ์ ์ฐจ๋์ด ์
์ฐจ๋๊ฑฐ๋ ์ถ์ฐจ๋ ์๊ฐ์ ๋ํ๋ด๋ฉฐ, HH:MM ํ์์ ๊ธธ์ด 5์ธ ๋ฌธ์์ด์
๋๋ค.
- HH:MM์ 00:00๋ถํฐ 23:59๊น์ง ์ฃผ์ด์ง๋๋ค.
- ์๋ชป๋ ์๊ฐ("25:22", "09:65" ๋ฑ)์ ์ ๋ ฅ์ผ๋ก ์ฃผ์ด์ง์ง ์์ต๋๋ค.
- ์ฐจ๋๋ฒํธ๋ ์๋์ฐจ๋ฅผ ๊ตฌ๋ถํ๊ธฐ ์ํ, `0'~'9'๋ก ๊ตฌ์ฑ๋ ๊ธธ์ด 4์ธ ๋ฌธ์์ด์ ๋๋ค.
- ๋ด์ญ์ ๊ธธ์ด 2 ๋๋ 3์ธ ๋ฌธ์์ด๋ก, IN ๋๋ OUT์ ๋๋ค. IN์ ์ ์ฐจ๋ฅผ, OUT์ ์ถ์ฐจ๋ฅผ ์๋ฏธํฉ๋๋ค.
- records์ ์์๋ค์ ์๊ฐ์ ๊ธฐ์ค์ผ๋ก ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌ๋์ด ์ฃผ์ด์ง๋๋ค.
- records๋ ํ๋ฃจ ๋์์ ์ /์ถ์ฐจ๋ ๊ธฐ๋ก๋ง ๋ด๊ณ ์์ผ๋ฉฐ, ์ ์ฐจ๋ ์ฐจ๋์ด ๋ค์๋ ์ถ์ฐจ๋๋ ๊ฒฝ์ฐ๋ ์ ๋ ฅ์ผ๋ก ์ฃผ์ด์ง์ง ์์ต๋๋ค.
- ๊ฐ์ ์๊ฐ์, ๊ฐ์ ์ฐจ๋๋ฒํธ์ ๋ด์ญ์ด 2๋ฒ ์ด์ ๋ํ๋ด์ง ์์ต๋๋ค.
- ๋ง์ง๋ง ์๊ฐ(23:59)์ ์ ์ฐจ๋๋ ๊ฒฝ์ฐ๋ ์ ๋ ฅ์ผ๋ก ์ฃผ์ด์ง์ง ์์ต๋๋ค.
- ์๋์ ์๋ฅผ ํฌํจํ์ฌ, ์๋ชป๋ ์
๋ ฅ์ ์ฃผ์ด์ง์ง ์์ต๋๋ค.
- ์ฃผ์ฐจ์ฅ์ ์๋ ์ฐจ๋์ด ์ถ์ฐจ๋๋ ๊ฒฝ์ฐ
- ์ฃผ์ฐจ์ฅ์ ์ด๋ฏธ ์๋ ์ฐจ๋(์ฐจ๋๋ฒํธ๊ฐ ๊ฐ์ ์ฐจ๋)์ด ๋ค์ ์ ์ฐจ๋๋ ๊ฒฝ์ฐ
3. ๋ด ํ์ด
from datetime import datetime
import time
from collections import defaultdict
import math
def solution(fees, records):
answer = []
cars = defaultdict(list)
for i in records:
car_info = i.split(' ')
cars[car_info[1]].append(car_info[0]+' '+car_info[2])
cars = dict(sorted(cars.items()))
for i in cars:
i_fee = []
for j in range(len(cars[i])):
if 'IN' in cars[i][j]:
intime = datetime.strptime(cars[i][j].split(' ')[0],'%H:%M')
try:
outtime=datetime.strptime(cars[i][j+1].split(' ')[0],'%H:%M')
except:
outtime = datetime.strptime('23:59','%H:%M')
i_fee.append((outtime-intime).seconds//60)
if sum(i_fee)<fees[0]:
answer.append(fees[1])
else:
answer.append(int(fees[1]+math.ceil((sum(i_fee)-fees[0])/fees[2])*fees[3]))
return answer
1. ์ฐจ๋ ๋ฒํธ ๋ณ ์ ์ฐจ ์๊ฐ๊ณผ ์ถ์ฐจ ์๊ฐ์ ๋ฐ๊ธฐ ์ํด defaultdict(list)๋ก cars ๋์ ๋๋ฆฌ๋ฅผ ์์ฑ
-> ์ฐจ๋ ๋ฒํธ ๋ณ [์ ์ฐจ, ์ถ์ฐจ] ๋์ ๋๋ฆฌ ์ ์ธ
2. cars ๋์ ๋๋ฆฌ๋ฅผ key๊ฐ์ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌ
3. for ๋ฌธ์ ์ฌ์ฉํ์ฌ ์ฐจ๋ ๋ณ ์ฃผ์ฐจ ์๊ฐ์ ํฉ๊ณ์ฐ
-> IN ๊ธ์๊ฐ ๋ฌธ์์ด ์์ ์กด์ฌํ๋ฉด strptime()ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ์๊ฐ ๋ฐ์ดํฐ๋ก ๋ณํ
-> try~except ๋ฌธ์ ์ฌ์ฉํ์ฌ ์ ์ฐจ ๋ค์ ์ถ์ฐจ ๊ธฐ๋ก์ด ์กด์ฌํ๋ฉด ์ถ์ฐจ์๊ฐ์ผ๋ก outtime ์ ์ธ, ์ถ์ฐจ ๊ธฐ๋ก์ด ์กด์ฌํ์ง ์๋๋ค๋ฉด 23:59๋ก ์ ์ธ
4. ์ ์ฐจ์๊ฐ๊ณผ ์ถ์ฐจ์๊ฐ์ ์ฐจ๋ฅผ ๊ตฌํ ๋ค์ ๋ถ์ผ๋ก ๋ณํํจ
5. ์ฐจ๋ ๋ฒํธ๋ณ ์ด ์ฃผ์ฐจ์๊ฐ์ ํฉ์ ๊ตฌํ์ฌ ๊ธฐ๋ณธ ์๊ธ ์๊ฐ๋ณด๋ค ์ ์ผ๋ฉด ๊ธฐ๋ณธ์๊ธ์ผ๋ก answer list์ ์ถ๊ฐ
๊ธฐ๋ณธ ์๊ธ ์๊ฐ๋ณด๋ค ๋ง์ผ๋ฉด ๋จ์์๊ฐ์ผ๋ก ๋๋ ๋ค, ๋จ์์๊ฐ๋ณ ์๊ธ ๊ณฑํจ + ๊ธฐ๋ณธ์๊ธ ๊ณ์ฐํ์ฌ answer list์ ์ถ๊ฐํ์ฌ answer ๋ฐํ
4. ๋ค๋ฅธ ์ฌ๋์ ํ์ด
from collections import defaultdict
from math import ceil
class Parking:
def __init__(self, fees):
self.fees = fees
self.in_flag = False
self.in_time = 0
self.total = 0
def update(self, t, inout):
self.in_flag = True if inout=='IN' else False
if self.in_flag: self.in_time = str2int(t)
else: self.total += (str2int(t)-self.in_time)
def calc_fee(self):
if self.in_flag: self.update('23:59', 'out')
add_t = self.total - self.fees[0]
return self.fees[1] + ceil(add_t/self.fees[2]) * self.fees[3] if add_t >= 0 else self.fees[1]
def str2int(string):
return int(string[:2])*60 + int(string[3:])
def solution(fees, records):
recordsDict = defaultdict(lambda:Parking(fees))
for rcd in records:
t, car, inout = rcd.split()
recordsDict[car].update(t, inout)
return [v.calc_fee() for k, v in sorted(recordsDict.items())]
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())))