반응형
Notice
Recent Posts
Link
«   2025/02   »
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
Archives
Today
Total
관리 메뉴

500error

[Cos Pro 1급, Python] 3차 10번 : 밥먹고 머리자르고 본문

알고리즘/파이썬

[Cos Pro 1급, Python] 3차 10번 : 밥먹고 머리자르고

Internal Server Error 2024. 2. 29. 16:04
반응형

문제

미용실과 레스토랑이 예약을 받는 기준은 다음과 같습니다.
* 미용실
  * 인원수가 1명인 경우에만 예약받습니다.
  * 다른 손님과 예약 시간이 겹칠 수 없습니다.
* 레스토랑
  * 인원수가 2명 이상 8명 이하인 경우에만 예약받습니다.
  * 최대 두 팀까지 예약 시간이 겹칠 수 있습니다.
두 가게에서 예약을 받은 횟수를 계산하기 위해 다음과 같이 Customer, Shop, HairShop, Restaurant 클래스를 작성했습니다.
* Customer:
  * Customer : 고객을 나타내는 클래스입니다.
  * id : 고객 식별 번호를 나타냅니다.
  * time : 고객이 신청한 예약 시간을 나타냅니다.
  * num_of_people : 예약 인원 수를 나타냅니다.
* Shop :
  *  Shop : 가게를 나타내는 클래스입니다.
  * reserve_list : 가게에 예약한 고객 리스트입니다.
  * reserve : 고객을 매개변수로 받아, 예약 고객 리스트에 추가 후 true를 return합니다.
* HairShop :
  * HairShop : 미용실을 나타내는 클래스이며, Shop을 상속합니다.
  * reserve : 고객을 매개변수로 받아, 미용실의 예약 기준에 맞는지 검사 합니다. 예약을 받았다면 예약 고객 리스트에 추가 후 true를 return하고, 그렇지 않으면 false를 return 합니다.
* Restaurant :
  * Restaurant는 레스토랑을 나타내는 클래스이며, Shop을 상속합니다.
  * reserve : 고객을 매개변수로 받아, 레스토랑의 예약 기준에 맞는지 검사 합니다. 예약을 받았다면 예약 고객 리스트에 추가 후 true를 return하고, 그렇지 않으면 false를 return 합니다.
예약을 원하는 고객 정보가 담긴 리스트 customers와 shops가 매개변수로 주어질 때, 두 가게에서 예약받은 횟수를 return 하도록 solution 함수를 작성하려고 합니다. 위 클래스 구조를 참고하여 주어진 코드의 빈칸을 적절히 채워 전체 코드를 완성해주세요.

 

 

 

 

 

 

 

 

 

 

 

코드

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 Restaurant(Shop):
	def __init__(self):
		super().__init__()
		
	def reserve(self, customer):
		if (2 <= customer.num_of_people <= 8) != True:
			return False
		count = 0
		for r in self.reserve_list:
			if r.time == customer.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, "입니다.")

 

 

 

반응형
Comments