Last active
March 26, 2021 12:53
-
-
Save MMohan1/9eb63f50767f4b3a0d7dcf955f4a1c25 to your computer and use it in GitHub Desktop.
Multi Parking lot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
N level are there in multi story parking | |
X number slots are there on each level | |
to park a vehicle take reg number and color | |
Question which your program need to answer | |
1.Give me all slots for given a color | |
2.Give me slot number for a give red number | |
""" | |
class Vehicle: | |
def __init__(self, regno, color): | |
self.regno = regno | |
self.color = color | |
class Car(Vehicle): | |
def __init__(self, regno, color): | |
Vehicle.__init__(self, regno, color) | |
class ParkingLot: | |
def __init__(self, level): | |
self.capacity = 10 | |
self.level = level | |
self.occupied_slots = 0 | |
self.create_parking_slots(self.capacity) | |
def get_capacity(self): | |
return self.capacity | |
def create_parking_slots(self, capacity): | |
self.slots = [-1] * capacity | |
self.capacity = capacity | |
def is_parking_lot_full(self): | |
if self.capacity == self.occupied_slots: | |
return True | |
return False | |
def get_empty_slots_count(self): | |
if self.capacity: | |
return self.capacity - self.occupied_slots | |
def __get_slot_index(self, slotid): | |
return int(slotid.split(':')[-1]) | |
def __get_empty_slot(self): | |
for slotid, slot in enumerate(self.slots): | |
if slot == -1: | |
return slotid | |
def park(self, vehicle): | |
if not self.is_parking_lot_full(): | |
slotid = self.__get_empty_slot() | |
self.slots[slotid] = vehicle | |
self.occupied_slots += 1 | |
return f'{self.level}: {slotid}' | |
else: | |
return -1 | |
def get_slotid(self, vehicle): | |
for slotid, slot in enumerate(self.slots): | |
if slot == vehicle: | |
return f'{self.level}: {slotid}' | |
def leave(self, vehicle): | |
if self.occupied_slots > 0: | |
slotid = self.get_slotid(vehicle) | |
slot_index = self.__get_slot_index(slotid) | |
self.slots[slot_index] = -1 | |
self.occupied_slots -= 1 | |
def get_occupied_slots_with_color(self, color): | |
colored_slots = [] | |
if not self.occupied_slots > 0: | |
return colored_slots | |
for slot in self.slots: | |
if slot != -1 and slot.color == color: | |
colored_slots.append(slot) | |
return colored_slots | |
def get_occupied_slots_with_regno(self, regno): | |
regno_slots = [] | |
if not self.occupied_slots > 0: | |
return regno_slots | |
for slotid, slot in enumerate(self.slots): | |
if slot != -1 and slot.regno == regno: | |
return f'{self.level}: {slotid}' | |
return -1 | |
class ParkingLevel(): | |
def __init__(self, level): | |
self.level = level | |
self.parking = ParkingLot(self.level) | |
def get_empty_slots(self): | |
return self.parking.get_empty_slots_count() | |
def main(): | |
car1 = Car(1, 'black') | |
car2 = Car(2, 'black') | |
car3 = Car(3, 'blue') | |
car4 = Car(4, 'black') | |
car5 = Car(5, 'red') | |
car6 = Car(6, 'red') | |
car7 = Car(7, 'red') | |
car8 = Car(8, 'yello') | |
level = ParkingLevel(1) | |
pl = level.parking | |
slot1 = pl.park(car1) | |
slot2 = pl.park(car2) | |
slot3 = pl.park(car3) | |
slot4 = pl.park(car4) | |
slot5 = pl.park(car5) | |
# slot6 = pl.park(car6) | |
# assert slot6 == -1 | |
black_occupied_slots = pl.get_occupied_slots_with_color('black') | |
blue_occupied_slots = pl.get_occupied_slots_with_color('blue') | |
red_occupied_slots = pl.get_occupied_slots_with_color('red') | |
assert len(black_occupied_slots) == 3 | |
assert len(blue_occupied_slots) == 1 | |
assert len(red_occupied_slots) == 1 | |
pl.leave(car1) | |
black_occupied_slots = pl.get_occupied_slots_with_color('black') | |
assert len(black_occupied_slots) == 2 | |
slot_id = pl.get_occupied_slots_with_regno(4) | |
assert slot_id == '1: 3' | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment