Menu
CoddyTech

예약 데스크 설계하기

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

challenge icon

챌린지

초급

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

편집 가능한 파일: 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)]

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

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