livepython.net

257. Python Program to Book Hotel Rooms with Check-In and Check-Out

MediumPhase 2 · Practice

This one takes a few steps. You know everything it uses: while loops, a list with in, and split().

Overview

A hotel desk must know which rooms are free and who is in each room. When a guest arrives, the desk gives them a free room. When a guest leaves, the room is free again.

Problem Statement

Selin runs a small hotel with 5 rooms, numbered 101 to 105. All rooms start free. Read one command per line until a line says end. The commands are:

  • checkin <name>: give the guest the free room with the lowest number, and print <name> is in room <room>. Check two things first, in this order. If the guest is already in a room, print <name> is already in room <room>. If all 5 rooms are taken, print Sorry, the hotel is full.
  • checkout <name>: the guest leaves, and their room is free again. Print <name> left room <room>. If the name is not a guest, print <name> is not a guest.
  • status: print the 5 rooms in order, one per line, as <room>: <name>, or <room>: free for a free room.

For any other command, print Unknown command. Names must match exactly: Ines and ines are two different guests.

Examples

Example 1. Input:

checkin Elif
checkin Joon
checkin Paulo
checkout Joon
checkin Nia
status
end

gives:

Elif is in room 101
Joon is in room 102
Paulo is in room 103
Joon left room 102
Nia is in room 102
101: Elif
102: Nia
103: Paulo
104: free
105: free

Why: after Joon leaves, room 102 is the lowest free room, so Nia gets 102, not 104.

Example 2. Input:

checkin Ade
checkin Bree
checkin Cato
checkin Dov
checkin Esi
checkin Femi
checkin Cato
checkout Gia
end

gives:

Ade is in room 101
Bree is in room 102
Cato is in room 103
Dov is in room 104
Esi is in room 105
Sorry, the hotel is full
Cato is already in room 103
Gia is not a guest

Why: the first 5 guests fill every room, so there is no room for Femi. Cato is already in room 103, and that check comes before the full check. Gia never checked in.

Input

One command per line, with one space between the command and the name. Names are one word. The last line is always end. Use input() with no prompt text: the tests type the input for you, and a prompt would end up in your output.

Output

One line for each checkin, checkout or unknown command, and 5 lines for each status. The tests try 7 different inputs, also an empty hotel, a guest who checks out twice and rooms freed in the middle of a full hotel.

Try it first. The solution opens when you solve this question, or after 3 tries with Submit.

Finish this chapter firstChapter 12 · Working With Strings

This is the last question of Phase 2 · Practice. The next question starts Phase 3.

solution.py
# write your code here

EXPECTED OUTPUT

1Elif is in room 101
2Joon is in room 102
3Paulo is in room 103
4Joon left room 102
5Nia is in room 102
6101: Elif
7102: Nia
8103: Paulo
9104: free
10105: free

YOUR OUTPUT

Press Run to see your output here. Lines are compared with the expected output.