Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to design parking system in Python
Suppose you want to design a parking system. A parking lot has three different kinds of parking spaces − big, medium, and small. Each size has a fixed number of available slots. We'll create a class called ParkingSystem with two methods ?
constructor(big, medium, small) − Takes the number of slots available for different spaces and initializes the ParkingSystem object.
addCar(carType) − Checks whether there is a parking space of the given carType for the car that wants to park.
The three slot types big, medium, and small are represented by 1, 2, and 3 respectively. A car can only park if there's an available space matching its type. If no space is available, return False, otherwise park the car and return True.
Example Scenario
If there are 2 spaces for big cars, 0 spaces for medium cars, and 1 space for small cars, the constructor call would be ParkingSystem(2, 0, 1). Here's how the addCar calls would work ?
addCar(3)− Add one small car, returnsTrueaddCar(2)− No space for medium car, returnsFalseaddCar(3)− No more small car spaces, returnsFalseaddCar(1)− Add one big car, returnsTrueaddCar(1)− Add another big car, returnsTrueaddCar(1)− No more big car spaces, returnsFalse
Implementation
Here's the complete implementation of the parking system ?
class ParkingSystem:
def __init__(self, big, medium, small):
# Index 0 is unused, 1=big, 2=medium, 3=small
self.spaces = [0, big, medium, small]
def addCar(self, carType):
if self.spaces[carType] > 0:
self.spaces[carType] -= 1
return True
return False
# Test the parking system
parking = ParkingSystem(2, 0, 1)
print("Adding small car:", parking.addCar(3))
print("Adding medium car:", parking.addCar(2))
print("Adding small car again:", parking.addCar(3))
print("Adding big car:", parking.addCar(1))
print("Adding another big car:", parking.addCar(1))
print("Adding third big car:", parking.addCar(1))
Adding small car: True Adding medium car: False Adding small car again: False Adding big car: True Adding another big car: True Adding third big car: False
How It Works
The solution uses a simple array to track available spaces. The spaces array has 4 elements where index 0 is unused, and indices 1, 2, 3 correspond to big, medium, and small car spaces respectively. When a car is added successfully, we decrement the available count for that car type.
Alternative Implementation
You can also use a dictionary for better readability ?
class ParkingSystemDict:
def __init__(self, big, medium, small):
self.spaces = {1: big, 2: medium, 3: small}
def addCar(self, carType):
if self.spaces[carType] > 0:
self.spaces[carType] -= 1
return True
return False
# Test dictionary version
parking_dict = ParkingSystemDict(1, 1, 1)
print("Big car:", parking_dict.addCar(1))
print("Medium car:", parking_dict.addCar(2))
print("Small car:", parking_dict.addCar(3))
print("Another big car:", parking_dict.addCar(1))
Big car: True Medium car: True Small car: True Another big car: False
Conclusion
The parking system efficiently manages different car sizes using a simple array or dictionary to track available spaces. The solution has O(1) time complexity for adding cars and uses minimal space to store the parking state.
