Obj Flashcards
1
Q
Design Parking Lot (Full)
A
class Vehicle: def \_\_init\_\_(self, spot_size): self._spot_size = spot_size def get_spot_size(self): return self._spot_size class Driver: def \_\_init\_\_(self, id, vehicle): self._id = id self._vehicle = vehicle self._payment_due = 0 def get_vehicle(self): return self._vehicle def get_id(self): return self._id def charge(self, amount): self._payment_due += amount class Car(Vehicle): def \_\_init\_\_(self): super().\_\_init\_\_(1) class Limo(Vehicle): def \_\_init\_\_(self): super().\_\_init\_\_(2) class SemiTruck(Vehicle): def \_\_init\_\_(self): super().\_\_init\_\_(3) class ParkingFloor: def \_\_init\_\_(self, spot_count): self._spots = [0]*spot_count self._vehicle_map = {} def park_vehicle(self, vehicle): size = vehicle.get_spot_size() l, r = 0, 0 while r < len(self._spots): if self._spots[r] != 0: l = r + 1 if r - l + 1 == size: # we found enough spots, park the vehicle for k in range(l, r+1): self._spots[k] = 1 self._vehicle_map[vehicle] = [l, r] return True r += 1 return False def remove_vehicle(self, vehicle): start, end = self._vehicle_map[vehicle] for i in range(start, end + 1): self._spots[i] = 0 del self._vehicle_map[vehicle] def get_parking_spots(self): return self._spots def get_vehicle_spots(self, vehicle): return self._vehicle_map.get(vehicle) class ParkingGarage: def \_\_init\_\_(self, floor_count, spots_per_floor): self._parking_floors = [ParkingFloor(spots_per_floor) for _ in range(floor_count)] def park_vehicle(self, vehicle): for floor in self._parking_floors: if floor.park_vehicle(vehicle): return True return False def remove_vehicle(self, vehicle): for floor in self._parking_floors: if floor.get_vehicle_spots(vehicle): floor.remove_vehicle(vehicle) return True return False import datetime import math class ParkingSystem: def \_\_init\_\_(self, parkingGarage, hourlyRate): self._parkingGarage = parkingGarage self._hourlyRate = hourlyRate self._timeParked = {} # map driverId to time that they parked def park_vehicle(self, driver): currentHour = datetime.datetime.now().hour isParked = self._parkingGarage.park_vehicle(driver.get_vehicle()) if isParked: self._timeParked[driver.get_id()] = currentHour return isParked def remove_vehicle(self, driver): if driver.get_id() not in self._timeParked: return False currentHour = datetime.datetime.now().hour timeParked = math.ceil(currentHour - self._timeParked[driver.get_id()]) driver.charge(timeParked * self._hourlyRate) del self._timeParked[driver.get_id()] return self._parkingGarage.remove_vehicle(driver.get_vehicle()) parkingGarage = ParkingGarage(3, 2) parkingSystem = ParkingSystem(parkingGarage, 5) driver1 = Driver(1, Car()) driver2 = Driver(2, Limo()) driver3 = Driver(3, SemiTruck()) print(parkingSystem.park_vehicle(driver1)) # true print(parkingSystem.park_vehicle(driver2)) # true print(parkingSystem.park_vehicle(driver3)) # false print(parkingSystem.remove_vehicle(driver1)) # true print(parkingSystem.remove_vehicle(driver2)) # true print(parkingSystem.remove_vehicle(driver3)) # false
2
Q
Vehicle Class
A
class Vehicle: def \_\_init\_\_(self, spot_size): self._spot_size = spot_size def get_spot_size(self): return self._spot_size
3
Q
Driver Class
A
class Driver: def \_\_init\_\_(self, id, vehicle): self._id = id self._vehicle = vehicle self._payment_due = 0 def get_vehicle(self): return self._vehicle def get_id(self): return self._id def charge(self, amount): self._payment_due += amount
4
Q
Cars, Limos, Semitrucks
A
class Car(Vehicle): def \_\_init\_\_(self): super().\_\_init\_\_(1) class Limo(Vehicle): def \_\_init\_\_(self): super().\_\_init\_\_(2) class SemiTruck(Vehicle): def \_\_init\_\_(self): super().\_\_init\_\_(3)
5
Q
Parking Floor Class
A
class ParkingFloor: def \_\_init\_\_(self, spot_count): self._spots = [0] * spot_count self._vehicle_map = {} def park_vehicle(self, vehicle): size = vehicle.get_spot_size() l, r = 0, 0 while r < len(self._spots): if self._spots[r] != 0: l = r + 1 if r - l + 1 == size: # we found enough spots, park the vehicle for k in range(l, r+1): self._spots[k] = 1 self._vehicle_map[vehicle] = [l, r] return True r += 1 return False def remove_vehicle(self, vehicle): start, end = self._vehicle_map[vehicle] for i in range(start, end + 1): self._spots[i] = 0 del self._vehicle_map[vehicle] def get_parking_spots(self): return self._spots def get_vehicle_spots(self, vehicle): return self._vehicle_map.get(vehicle)
6
Q
Parking Garage Class
A
class ParkingGarage: def \_\_init\_\_(self, floor_count, spots_per_floor): self._parking_floors = [ParkingFloor(spots_per_floor) for _ in range(floor_count)] def park_vehicle(self, vehicle): for floor in self._parking_floors: if floor.park_vehicle(vehicle): return True return False def remove_vehicle(self, vehicle): for floor in self._parking_floors: if floor.get_vehicle_spots(vehicle): floor.remove_vehicle(vehicle) return True return False