Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions module-10/calculator.py
Original file line number Diff line number Diff line change
@@ -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")
24 changes: 24 additions & 0 deletions module-10/test_calculator.py
Original file line number Diff line number Diff line change
@@ -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)
131 changes: 131 additions & 0 deletions module-10/vehicle/ParkingLot.py
Original file line number Diff line number Diff line change
@@ -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()
12 changes: 12 additions & 0 deletions module-10/vehicle/Vehicle.py
Original file line number Diff line number Diff line change
@@ -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"
34 changes: 34 additions & 0 deletions module-10/vehicle/test_parkingLot.py
Original file line number Diff line number Diff line change
@@ -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")