11import sys
22from dataclasses import dataclass
33from enum import Enum
4- from typing import List
4+ from typing import List , Tuple
55from collections import Counter
66
77class OperatingSystem (Enum ):
@@ -58,7 +58,7 @@ def find_possible_laptops(available_laptops: List[Laptop], current_person: Perso
5858
5959def user_prompt () -> Person :
6060 try :
61- name = str (input ("Please enter your first name: " ))
61+ name = (input ("Please enter your first name: " )). strip ( )
6262 if not name .isalpha ():
6363 raise ValueError ("Name must contain only alphabetic characters." )
6464
@@ -97,7 +97,7 @@ def main() -> None:
9797 matching_laptops = find_possible_laptops (laptops , user_details )
9898
9999 # get the counts of the laptops for an OS e.g. os_counts = {OperatingSystem.MACOS: 4,OperatingSystem.ARCH: 2,OperatingSystem.UBUNTU: 3}
100- os_counts = Counter (laptop .operating_system for laptop in laptops )
100+ os_counts : Counter [ OperatingSystem ] = Counter (laptop .operating_system for laptop in laptops )
101101
102102 print (f"\n { user_details .name } , there are { len (matching_laptops )} laptops available with your preferred operating system ({ user_details .preferred_operating_systems .value } ).\n " )
103103
@@ -111,9 +111,9 @@ def main() -> None:
111111 # get the count of laptops for users preferred OS
112112 user_os_count = os_counts [user_details .preferred_operating_systems ]
113113
114- # find the OSes with more laptops available
115- alternative_os = [(os , count ) for os , count in os_counts .items () if count > user_os_count ]
116-
114+ # sort alternative OS so most available OS is listed first
115+ alternative_os = sorted ( [(os , count ) for os , count in os_counts .items () if count > user_os_count ], key = lambda x : x [ 1 ], reverse = True )
116+
117117 if alternative_os :
118118 print ("\n However, there are more laptops available with the following operating systems:" )
119119 for os , count in alternative_os :
0 commit comments