262. Python Program to Make a Parking Lot with Classes
HardPhase 3 · Challenge
This one has more steps, but you have learned every part: classes, methods, __str__ and a dictionary. Draw 3 slots on paper and park a few cars first.
Overview
Parking lots with a screen at the gate know which slots are free. The program behind the screen is a small set of objects: one for the parking lot and one for each car.
Problem Statement
Mariam runs a small parking lot. The slots are numbered from 1 to N. A new car always gets the lowest free slot.
Write the program with two classes:
Car
__init__(self, plate, slot): saves the number plate and the slot number.__str__(self): returnsSlot <slot>: <plate>, likeSlot 2: AB123.
ParkingLot
__init__(self, size): saves the number of slots. At the start, no car is parked.find(self, plate): returns the slot of the car with this plate, or 0 if that car is not here.park(self, plate): makes a newCarin the lowest free slot and returns the slot. If no slot is free, it returns 0.leave(self, plate): takes the car out and returns its slot, or 0 if that car is not here.status(self): prints one line for each slot, from slot 1 up: the car's__str__text, orSlot <n>: free.
The program reads the number of slots, then one command per line, until a line says end:
park <plate>: if the car is already parked, print<plate> is already parked. Else, if no slot is free, printSorry, the parking lot is full. Else, park it and print<plate> parked in slot <n>.leave <plate>: print<plate> left slot <n>, or<plate> is not hereif the car is not parked.status: callstatus().
Examples
Example 1. Input:
3
park AB123
park CD456
park EF789
park GH111
leave CD456
park GH111
status
end
gives:
AB123 parked in slot 1
CD456 parked in slot 2
EF789 parked in slot 3
Sorry, the parking lot is full
CD456 left slot 2
GH111 parked in slot 2
Slot 1: AB123
Slot 2: GH111
Slot 3: EF789
Why: the 3 slots fill up, so GH111 cannot park the first time. When CD456 leaves slot 2, GH111 gets slot 2, the only free one.
Example 2. Input:
2
park KX10
park KX10
leave ZZ99
status
leave KX10
status
end
gives:
KX10 parked in slot 1
KX10 is already parked
ZZ99 is not here
Slot 1: KX10
Slot 2: free
KX10 left slot 1
Slot 1: free
Slot 2: free
Why: the same car cannot park twice, and a car that never came in cannot leave. status shows every slot, also the free ones.
Input
The first line is the number of slots, from 1 to 9. Then one command per line, in the formats above, and a last line end. A plate is one word of capital letters and digits, like AB123. 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 park and leave, and one line per slot for each status. The tests try 7 different inputs, also a parking lot with 1 slot, a car that tries to park twice when the lot is full, and two free slots where the new car must take the lower one.
Try it first. The solution opens when you solve this question, or after 3 tries with Submit.