249. Python Program to Book Cinema Seats with a Seat Map
MediumPhase 2 · Practice
This one takes a few steps. You know everything it uses: a while loop, a list, nested for loops and string indexes.
Overview
Cinema apps show a seat map before you pay. You pick a free seat, and the app marks it as booked so nobody else can take it.
Problem Statement
Arjun runs a tiny cinema hall. It has 3 rows, A, B and C, with 5 seats in each row, numbered 1 to 5. A seat code is the row letter and then the seat number, like B3. So the 15 seats are A1 to A5, B1 to B5 and C1 to C5. All seats start free.
Read one command per line until a line says end. The commands are:
book <seat>: book that seat and printBooked <seat>. If the seat is booked already, print<seat> is already booked. If the code is not one of the 15 seats, likeD2,C6orB10, printNo seat <seat>.map: print the hall, one line per row, from rowAto rowC. Each line starts with the row letter. Then come the 5 seats in order, each after one space:O(the capital letter O) for a free seat andXfor a booked seat. For example,A O O X O Omeans that in row A only seat A3 is booked.
For any other command, print Unknown command. After end, print Free seats: <count>: how many of the 15 seats are still free.
Examples
Example 1. Input:
book A3
book B1
book A3
map
end
gives:
Booked A3
Booked B1
A3 is already booked
A O O X O O
B X O O O O
C O O O O O
Free seats: 13
Why: A3 and B1 are booked, so the map shows an X in those two places. The second book A3 is refused. 15 - 2 = 13 seats are free.
Example 2. Input:
book D2
book C6
book C5
map
sell C5
end
gives:
No seat D2
No seat C6
Booked C5
A O O O O O
B O O O O O
C O O O O X
Unknown command
Free seats: 14
Why: there is no row D and no seat 6, so D2 and C6 are not seats. C5 is the last seat of the last row. sell is not a command.
Input
One command per line. After book comes one word: a seat code in capital letters, or any other short text without spaces. 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 book or unknown command, 3 lines for each map, then the Free seats: line. The tests try 7 different inputs, also a full hall and codes like B10, B0 and B.
Try it first. The solution opens when you solve this question, or after 3 tries with Submit.