Menu
CoddyTech

예약 접수 시스템 설계하기

Coddy의 Go 개발자를 위한 Python 코스 레슨. 48개 중 48번째.

challenge icon

챌린지

초급

독립적인 capacity와 bookings(이름을 좌석 수에 대응시키는 dict), reserve(name,seats), cancel(name)을 갖는 Room(capacity)을 구현하세요. 작업에 실패하면 상태를 변경하지 않고 ValueError를 발생시킵니다. reserve는 양의 정수 seats, 새로운 이름, 충분한 잔여 수용 인원을 요구합니다. cancel은 기존 이름을 요구합니다. BookingDesk(room)는 전달받은 room을 저장합니다. handle(command)는 reserve/cancel을 위임하고 ValueError를 처리하여 ok/invalid를 반환하며, 절대 출력하지 않습니다. main()은 음이 아닌 수용 인원, 음이 아닌 명령 개수, 이어서 reserve name seats 또는 cancel name 형식의 명령을 읽습니다. 이름은 비어 있지 않은 소문자 ASCII 토큰입니다. 형식이 잘못된 정수, 필드 개수가 맞지 않는 경우, 알 수 없거나 비어 있는 명령, 중복 예약, 수용 인원 부족을 거부하세요. 각 명령마다 ok/invalid를 출력한 다음 available:N과 이름순으로 정렬된 활성 name:seats 행을 출력하세요. 취소된 이름은 다시 예약할 수 있습니다. 수용 인원이 0이면 어떤 예약도 받을 수 없습니다. Go 인터페이스 협력 객체와 마찬가지로 현재 room을 통해 위임하세요. 잠긴 드라이버는 desk.room을 프로브로 대체하고 호출 및 상태 보존을 확인합니다. 모든 데이터는 메모리에 저장됩니다.

편집 가능한 파일: room.py, booking_desk.py, main.py. 제공된 import와 필수 API 이름을 유지하세요. 잠긴 driver.py가 실행과 검사를 담당하므로 수정하지 마세요.

room.py에는 Room을, booking_desk.py에는 BookingDesk를 배치하고, main.py의 main() 안에 입력/출력 코드를 작성하세요. 제공된 import와 진입 래퍼를 유지하세요. 데스크는 대체 객체를 생성하는 대신 전달된 현재 room을 사용합니다.

직접 해보기

from room import Room
from booking_desk import BookingDesk
from main import main

main()

probe_room = Room(4)
other_room = Room(4)
probe_room.reserve("a", 3)
assert probe_room.capacity == 4 and probe_room.bookings == {"a": 3}
assert other_room.bookings == {}
for name, seats in [("a", 1), ("b", 2), ("c", 0), ("d", -1)]:
    try:
        probe_room.reserve(name, seats)
    except ValueError:
        pass
    else:
        raise AssertionError("Invalid reservation accepted")
assert probe_room.bookings == {"a": 3}
try:
    probe_room.cancel("missing")
except ValueError:
    pass
else:
    raise AssertionError("Unknown cancellation accepted")
probe_room.cancel("a")
assert probe_room.bookings == {}
probe_desk = BookingDesk(other_room)
assert probe_desk.room is other_room
assert probe_desk.handle("reserve x 4") == "ok"
assert other_room.bookings == {"x": 4}
class ProbeRoom:
    def __init__(self):
        self.calls = []
    def reserve(self, name, seats):
        self.calls.append(("reserve", name, seats))
        if seats == 9:
            raise ValueError("probe rejection")
    def cancel(self, name):
        self.calls.append(("cancel", name))
probe = ProbeRoom()
probe_desk.room = probe
assert probe_desk.handle("reserve y 2") == "ok"
assert probe_desk.handle("cancel y") == "ok"
assert probe_desk.handle("reserve z 9") == "invalid"
assert probe_desk.handle("reserve a bad") == "invalid"
assert probe.calls == [("reserve", "y", 2), ("cancel", "y"), ("reserve", "z", 9)]

Go 개발자를 위한 Python의 모든 레슨

직접 연습해 보세요: 온라인 Python 컴파일러