@@ -44,14 +44,13 @@ class Laptop:
4444 Person (name = "Sara" , age = 28 , preferred_operating_system = [OperatingSystem .MACOS ])
4545]
4646
47- # Global sadness counter
48- sadness = 0
4947
5048# Allocate laptops to people to minimize total sadness
5149def allocate_laptops (people : List [Person ], laptops : List [Laptop ]) -> Dict [Person , Laptop ]:
5250 sorted_people_OS_count = sorted (people ,key = lambda p :len (p .preferred_operating_system ))
51+ sadness = 0 # local variable sadness counter
5352 allocated_history : Dict [Person ,Laptop ] = {}
54- global sadness
53+
5554 for person in sorted_people_OS_count :
5655 allocated_flag = False
5756 for i in range (len (person .preferred_operating_system )) :
@@ -70,11 +69,13 @@ def allocate_laptops(people: List[Person], laptops: List[Laptop]) -> Dict[Person
7069 laptops .remove (laptops [0 ])
7170 sadness += 100 # high sadness for non-preferred OS
7271
73- return allocated_history
72+ return allocated_history , sadness
7473
75- def print_final_allocation (allocated_history :dict [Person ,Laptop ]) :
74+ def print_final_allocation (allocated_history :dict [Person ,Laptop ], sadness : int ) :
7675 for name , laptop in allocated_history .items () :
7776 print (f"{ name :<10} : Laptop Id { laptop .id :<3} - OS({ laptop .operating_system .name } ) " )
7877 print (f"Total sadness is : { sadness } " )
7978
80- print_final_allocation (allocate_laptops (people ,laptops ))
79+ allocated_history , sadness = allocate_laptops (people , laptops )
80+
81+ print_final_allocation (allocated_history , sadness )
0 commit comments