@@ -55,12 +55,13 @@ class Laptop:
5555 Person (name = "Ger" , age = 51 , preferred_operating_systems = [OperatingSystem .UBUNTU , OperatingSystem .MACOS ]),
5656]
5757
58+
59+ # updated user prompt to include order of preference in selecting OS
5860def user_prompt () -> Person :
5961 """
6062 prompt the user to input their details and preferred operating systems
6163 """
6264 try :
63-
6465 # strip() whitespace before processing (no need for str type here as input always returns a string)
6566 name = input ("Please enter your first name: " ).strip ()
6667 if not name .isalpha ():
@@ -88,56 +89,45 @@ def user_prompt() -> Person:
8889 for os_name in preferred_os_list :
8990 if os_name not in valid_os :
9091 raise ValueError (f"Invalid operating system: { os_name } " )
91- # convert to enum
92+ # convert to enum
9293 preferred_os_enum .append (OperatingSystem (os_name ))
9394
9495 return Person (name = name , age = age , preferred_operating_systems = tuple (preferred_os_enum ))
9596
9697 # throw an error and exit for invalid age and os input
9798 except ValueError as error :
9899 print (f"Invalid input: { error } " , file = sys .stderr )
99- sys .exit (1 )
100100
101101
102- def find_possible_laptops (available_laptops : List [Laptop ], current_person : Person ) -> List [Laptop ]:
103- """
104- find laptops that match a person's preferred operating systems.
105- """
106- return [
107- laptop for laptop in available_laptops
108- if laptop .operating_system in current_person .preferred_operating_systems
109- ]
110-
111102def sadness_score (person : Person , laptop : Laptop ) -> int :
112103 """
113104 calculate the sadness score for a person based on the allocated laptop.
114105 """
115- try :
106+ if laptop . operating_system in person . preferred_operating_systems :
116107 return person .preferred_operating_systems .index (laptop .operating_system )
117- except ValueError :
118- return 100
119-
108+ return 100
120109
121110def allocate_laptops (people : List [Person ], laptops : List [Laptop ]) -> Dict [Person , Laptop ]:
122111 """
123112 allocate laptops to people to minimize total sadness.
124113 """
125- allocated_laptops = {}
114+ allocated_laptops : Dict [ str , Laptop ] = {}
126115
127116 # create a shallow copy of the laptops list
128117 available_laptops = laptops [:]
129118
130- for person in people :
119+ sorted_people = sorted (people , key = lambda p : len (p .preferred_operating_systems ))
120+
121+ for person in sorted_people :
131122 # ensure available_laptops is not empty before calling min
132123 if not available_laptops :
133124 raise ValueError ("No laptops available to allocate." )
134125
135126 # use min() to find the laptop that minimizes their 'sadness score'
136127 # lambda function is scoring the person's preferences by calculating the 'sadness score' for each laptop based on the index position
137- best_laptop = min (available_laptops , key = lambda laptop : sadness_score (person , laptop ), default = None )
138- if best_laptop :
139- allocated_laptops [person .name ] = best_laptop
140- available_laptops .remove (best_laptop )
128+ best_laptop = min (available_laptops , key = lambda laptop : sadness_score (person , laptop ))
129+ allocated_laptops [person .name ] = best_laptop
130+ available_laptops .remove (best_laptop )
141131
142132
143133 if len (allocated_laptops ) != len (people ):
@@ -147,15 +137,24 @@ def allocate_laptops(people: List[Person], laptops: List[Laptop]) -> Dict[Person
147137
148138
149139def main ():
150- # Prompt the user for their details and add them to the people list
151- new_person = user_prompt ()
152- people .append (new_person )
153-
154- # Allocate laptops and print the results
155- allocation = allocate_laptops (people , laptops_list )
156- for name , laptop in allocation .items ():
157- person_sadness_score = sadness_score (next (person for person in people if person .name == name ), laptop )
158- print (f"{ name } was allocated { laptop .manufacturer } { laptop .model } with { laptop .operating_system .value } (Score: { person_sadness_score } )" )
140+ """
141+ allocate laptops and display results
142+ """
143+ try :
144+ # prompt the user for their details and add them to the people list
145+ new_person = user_prompt ()
146+ people .append (new_person )
147+
148+ # allocate laptops and print the results
149+ allocation = allocate_laptops (people , laptops_list )
150+
151+ for name , laptop in allocation .items ():
152+ person = next (person for person in people if person .name == name )
153+ person_sadness_score = sadness_score (person , laptop )
154+ print (f"{ name } was allocated { laptop .manufacturer } { laptop .model } with { laptop .operating_system .value } (Score: { person_sadness_score } )" )
155+
156+ except Exception as error :
157+ print (f"An error occurred: { error } " , file = sys .stderr )
159158
160159# Ensure the script runs only when executed directly
161160if __name__ == "__main__" :
0 commit comments