Skip to content

Commit 6ebb6fe

Browse files
committed
refactoring code & fixing bugs
1 parent 1fe0e49 commit 6ebb6fe

1 file changed

Lines changed: 13 additions & 8 deletions

File tree

laptop_allocation.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@ class Laptop:
3535
Person(name="Leila", age=45, preferred_operating_system=[OperatingSystem.MACOS,OperatingSystem.UBUNTU,OperatingSystem.ARCH]),
3636
Person(name="Mary", age=35, preferred_operating_system=[OperatingSystem.MACOS,OperatingSystem.ARCH]),
3737
]
38+
sadness=0
3839
def allocate_laptops(people: List[Person], laptops: List[Laptop]) -> Dict[Person, Laptop]:
40+
sorted_people_OS_count=sorted(people,key=lambda p:len(p.preferred_operating_system))
3941
allocated_history : Dict[Person,Laptop] ={}
40-
sadness=0
41-
for person in people :
42+
global sadness
43+
for person in sorted_people_OS_count :
4244
allocated_flag=False
4345
for i in range(len(person.preferred_operating_system)) :
4446
for laptop in laptops :
@@ -56,9 +58,12 @@ def allocate_laptops(people: List[Person], laptops: List[Laptop]) -> Dict[Person
5658
allocated_history[person.name]=laptops[0]
5759
laptops.remove(laptops[0])
5860
sadness +=100
59-
60-
61-
62-
return allocated_history ,sadness
63-
64-
print(allocate_laptops(people,laptops))
61+
62+
return allocated_history
63+
64+
def print_final_allocation(allocated_history:dict[Person,Laptop]) :
65+
for name , laptop in allocated_history.items() :
66+
print(f"{name:<10} : Laptop Id {laptop.id:<3} - OS({laptop.operating_system}) ")
67+
print(f"Total sadness is : {sadness}")
68+
69+
print_final_allocation(allocate_laptops(people,laptops))

0 commit comments

Comments
 (0)