
미용실과 레스토랑이 예약을 받는 기준은 다음과 같습니다.
미용실
레스토랑
두 가게에서 예약을 받은 횟수를 계산하기 위해 다음과 같이 Customer, Shop, HairShop, Restaurant 클래스를 작성했습니다.

Customer:
Shop :
HairShop :
Restaurant :
예약을 원하는 고객 정보가 담긴 리스트 customers와 shops가 매개변수로 주어질 때, 두 가게에서 예약받은 횟수를 return 하도록 solution 함수를 작성하려고 합니다. 위 클래스 구조를 참고하여 주어진 코드의 빈칸을 적절히 채워 전체 코드를 완성해주세요.
예약을 원하는 고객 정보가 담긴 리스트 customers와 shops가 solution 함수의 매개변수로 주어집니다.
두 가게에서 예약받은 횟수를 return 해주세요.
| customers | shops | return |
|---|---|---|
| [[1000, 2, 1], [2000, 2, 4], [1234, 5, 1], [4321, 2, 1], [1111, 3, 10]] | ["hairshop", "restaurant", "hairshop", "hairshop", "restaurant"] | 3 |
고객별 예약 정보는 다음과 같습니다.
| ID | 예약 시간 | 인원수 | 가게 |
|---|---|---|---|
| 1000 | 2 | 1 | hairshop |
| 2000 | 2 | 4 | restaurant |
| 1234 | 5 | 1 | hairshop |
| 4321 | 2 | 1 | hairshop |
| 1111 | 3 | 10 | restaurant |
class Customer:
def __init__(self, id, time, num_of_people):
self.id = id
self.time = time
self.num_of_people = num_of_people
class Shop:
def __init__(self):
self.reserve_list = []
def reserve(self, customer):
self.reserve_list.append(customer)
return True
class HairShop@@@:
def __init__(self):
super().__init__()
@@@:
if @@@ != 1:
return False
for r in self.reserve_list:
if @@@:
return False
self.reserve_list.append(customer)
return True
class Restaurant@@@:
def __init__(self):
super().__init__()
@@@:
if @@@:
return False
count = 0
for r in self.reserve_list:
if @@@:
count += 1
if count >= 2:
return False
self.reserve_list.append(customer)
return True
def solution(customers, shops):
hairshop = HairShop()
restaurant = Restaurant()
count = 0
for customer, shop in zip(customers, shops):
if shop == "hairshop":
if hairshop.reserve(Customer(customer[0], customer[1], customer[2])):
count += 1
elif shop == "restaurant":
if restaurant.reserve(Customer(customer[0], customer[1], customer[2])):
count += 1
return count
#아래는 테스트케이스 출력을 해보기 위한 코드입니다.
customers = [[1000, 2, 1],[2000, 2, 4],[1234, 5, 1],[4321, 2, 1],[1111, 3, 10]]
shops = ["hairshop", "restaurant", "hairshop", "hairshop", "restaurant"]
ret = solution(customers, shops)
#[실행] 버튼을 누르면 출력 값을 볼 수 있습니다.
print("solution 함수의 반환 값은", ret, "입니다.")
이번엔 빈칸 채우기 문제이다.
class Customer:
def __init__(self,id,time,num_of_people):
self.id=id
self.time=time
self.num_of_people=num_of_people
class Shop:
def __init__(self):
self.reserve_list=[]
def reserve(self,customer):
self.reserve_list.append(customer)
return True
class HairShop(Shop):
def __init__(self):
super().__init__()
def reserve(self, customer):
if customer.num_of_people != 1:
return False
for r in self.reserve_list:
if customer.time == r.time:
return False
self.reserve_list.append(customer)
return True
class Restaurant(Shop):
def __init__(self):
super().__init__()
def reserve(self, customer):
if 2 > customer.num_of_people or customer.num_of_people > 8:
return False
count = 0
for r in self.reserve_list:
if customer.time == r.time:
count += 1
if count >= 2:
return False
self.reserve_list.append(customer)
return True
def solution(customers, shops):
hairshop = HairShop()
restaurant = Restaurant()
count = 0
for customer, shop in zip(customers, shops):
if shop == "hairshop":
if hairshop.reserve(Customer(customer[0], customer[1], customer[2])):
count += 1
elif shop == "restaurant":
if restaurant.reserve(Customer(customer[0], customer[1], customer[2])):
count += 1
return count
#아래는 테스트케이스 출력을 해보기 위한 코드입니다.
customers = [[1000, 2, 1],[2000, 2, 4],[1234, 5, 1],[4321, 2, 1],[1111, 3, 10]]
shops = ["hairshop", "restaurant", "hairshop", "hairshop", "restaurant"]
ret = solution(customers, shops)
#[실행] 버튼을 누르면 출력 값을 볼 수 있습니다.
print("solution 함수의 반환 값은", ret, "입니다.")
해당 문제는 일반 클래스인 Shop 클래스를 상속하는 HairShop 클래스와 Restaurant 클래스를 정의하는 코드를 완성하는 문제이다.
해당 문제에서 등장하는 상속개념을 정확히 알고 싶으면 아래의 포스트를 보면 된다😊
상속이란?