From 389964cc40736367c3c29e6ae73a531f788ab42f Mon Sep 17 00:00:00 2001 From: Deepak Kumar Date: Thu, 8 Jun 2023 23:02:16 +0530 Subject: [PATCH 1/2] module 10 done --- module-10/calculator.py | 14 ++++++++++++++ module-10/test_calculator.py | 24 ++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 module-10/calculator.py create mode 100644 module-10/test_calculator.py diff --git a/module-10/calculator.py b/module-10/calculator.py new file mode 100644 index 0000000..c7d7a18 --- /dev/null +++ b/module-10/calculator.py @@ -0,0 +1,14 @@ +def add(x, y): + return x + y + +def subtract(x, y): + return x - y + +def multiply(x, y): + return x * y + +def divide(x, y): + if y != 0: + return x / y + else: + raise ValueError("Cannot divide by zero") \ No newline at end of file diff --git a/module-10/test_calculator.py b/module-10/test_calculator.py new file mode 100644 index 0000000..20002d9 --- /dev/null +++ b/module-10/test_calculator.py @@ -0,0 +1,24 @@ +import pytest +from calculator import add, subtract, multiply, divide + +def test_add(): + assert add(2, 3) == 5 + assert add(-1, 5) == 4 + assert add(0, 0) == 0 + +def test_subtract(): + assert subtract(5, 2) == 3 + assert subtract(0, 5) == -5 + assert subtract(-10, -2) == -8 + +def test_multiply(): + assert multiply(3, 4) == 12 + assert multiply(0, 10) == 0 + assert multiply(-2, 5) == -10 + +def test_divide(): + assert divide(10, 2) == 5 + assert divide(0, 5) == 0 + assert divide(-12, -3) == 4 + with pytest.raises(ValueError): + divide(10, 0) From 05b4fa93c8fc290bc9ebe710990a0f97526c7ad5 Mon Sep 17 00:00:00 2001 From: Deepak Kumar Date: Fri, 9 Jun 2023 16:35:01 +0530 Subject: [PATCH 2/2] module 10 done --- module-10/vehicle/ParkingLot.py | 131 +++++++++++++++++++++++++++ module-10/vehicle/Vehicle.py | 12 +++ module-10/vehicle/test_parkingLot.py | 34 +++++++ 3 files changed, 177 insertions(+) create mode 100644 module-10/vehicle/ParkingLot.py create mode 100644 module-10/vehicle/Vehicle.py create mode 100644 module-10/vehicle/test_parkingLot.py diff --git a/module-10/vehicle/ParkingLot.py b/module-10/vehicle/ParkingLot.py new file mode 100644 index 0000000..cef32d1 --- /dev/null +++ b/module-10/vehicle/ParkingLot.py @@ -0,0 +1,131 @@ +import Vehicle + + +class ParkingLot: + def __init__(self): + self.capacity = 0 + self.slot_id = 0 + self.occupied_slot = 0 + + def create_parking_lot(self, capacity): + self.slots = [-1] * capacity + self.capacity = capacity + return self.capacity + + def get_empty_slot(self): + for i in range(len(self.slots)): + if self.slots[i] == -1: + return i + + def park(self, regno, color): + print("New Car parking: " + str(regno) + str(color), self.occupied_slot, self.capacity) + if self.occupied_slot < self.capacity: + slot_id = self.get_empty_slot() + self.slots[slot_id] = Vehicle.Car(regno, color) + self.slot_id = self.slot_id + 1 + self.occupied_slot = self.occupied_slot + 1 + return slot_id + 1 + else: + return -1 + + def leave_slot(self, slot_id): + print(self.slots, self.slots[slot_id - 1], self.occupied_slot) + if self.occupied_slot > 0 and self.slots[slot_id - 1] != -1: + self.slots[slot_id - 1] = -1 + self.occupied_slot = self.occupied_slot - 1 + print("Vacant slot: " + str(slot_id)) + return True + else: + print("Empty slot: " + str(slot_id)) + return False + + def check_status(self): + print("Slot No \tRegistration No \tColor") + for i in range(len(self.slots)): + if self.slots[i] != -1: + print(str(i + 1) + "\t" + str(self.slots[i].regno) + "\t" + str(self.slots[i].color)) + else: + continue + + def get_regno_from_color(self, color): + regnos = [] + for i in self.slots: + if i == -1: + continue + if i.color == color: + regnos.append(i.regno) + print("Register numbers: ", regnos) + return regnos + + def get_slotno_from_regno(self, regno): + for i in range(len(self.slots)): + if self.slots[i].regno == regno: + print(i+1) + return i+1 + else: + continue + return -1 + + def get_slotno_from_color(self, color): + slotnos = [] + for i in range(len(self.slots)): + if self.slots[i] == -1: + continue + if self.slots[i].color == color: + slotnos.append(str(i + 1)) + print("Slot numbers: ", slotnos) + return slotnos + + def show_data(self, line): + if line.startswith('create_parking_lot'): + n = int(line.split(' ')[1]) + res = self.create_parking_lot(n) + print('Created a parking lot with ' + str(res) + ' slots') + + elif line.startswith('park'): + regno = line.split(' ')[1] + color = line.split(' ')[2] + res = self.park(regno, color) + if res == -1: + print("parking lot is full") + else: + print('Allocated slot number: ' + str(res)) + elif line.startswith('leave_slot'): + leave_slot_id = int(line.split(' ')[1]) + status = self.leave_slot(leave_slot_id) + if status: + print('slot number ' + ' ' + str(leave_slot_id) + 'is free') + + elif line.startswith('status'): + self.check_status() + + elif line.startswith('regno_for_cars_with_colour'): + color = line.split(' ')[1] + regnos = self.get_regno_from_color(color) + print(', '.join(regnos)) + + elif line.startswith('slotno_for_cars_with_colour'): + color = line.split(' ')[1] + slotnos = self.get_slotno_from_color(color) + print(', '.join(slotnos)) + + elif line.startswith('slot_number_for_regno'): + regno = line.split(' ')[1] + slotno = self.get_slotno_from_regno(regno) + if slotno == -1: + print("Not found") + else: + print(slotno) + elif line.startswith('exit'): + exit(0) + + +def main(): + parkinglot = ParkingLot() + while True: + line = input(">> ") + parkinglot.show_data(line) + + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/module-10/vehicle/Vehicle.py b/module-10/vehicle/Vehicle.py new file mode 100644 index 0000000..0b1130c --- /dev/null +++ b/module-10/vehicle/Vehicle.py @@ -0,0 +1,12 @@ +class Vehicle: + def __init__(self, regno, color): + self.color=color + self.regno=regno + + +class Car(Vehicle): + def __init__(self, regno, color): + Vehicle.__init__(self, regno, color) + + def getType(self): + return "Car" \ No newline at end of file diff --git a/module-10/vehicle/test_parkingLot.py b/module-10/vehicle/test_parkingLot.py new file mode 100644 index 0000000..e360d28 --- /dev/null +++ b/module-10/vehicle/test_parkingLot.py @@ -0,0 +1,34 @@ +import pytest +from ParkingLot import ParkingLot +from Vehicle import Car + +@pytest.fixture +def parking_lot(): + return ParkingLot() + +def test_create_parking_lot(parking_lot): + assert parking_lot.create_parking_lot(5) == 5 + +def test_park(parking_lot): + parking_lot.create_parking_lot(5) + assert parking_lot.park("ABC123", "Red") == 1 + assert parking_lot.park("XYZ456", "Blue") == 2 + +def test_leave_slot(parking_lot): + parking_lot.create_parking_lot(5) + parking_lot.park("ABC123", "Red") + assert parking_lot.leave_slot(1) == True + +def test_check_status(parking_lot, capsys): + parking_lot.create_parking_lot(3) + parking_lot.park("ABC123", "Red") + parking_lot.park("XYZ789", "Blue") + parking_lot.check_status() + captured = capsys.readouterr() + assert "1\tABC123\tRed" in captured.out + assert "2\tXYZ789\tBlue" in captured.out + +def test_get_regno_from_color(parking_lot): + parking_lot.create_parking_lot(5) + parking_lot.park("ABC123", "Red") + parking_lot.park("XYZ456", "Blue")