Skip to content

Commit 27eb7d3

Browse files
committed
Laptop allocation.
1 parent 982f22f commit 27eb7d3

1 file changed

Lines changed: 88 additions & 0 deletions

File tree

‎laptop_allocation.py‎

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import sys
2+
from dataclasses import dataclass
3+
from enum import Enum
4+
from typing import List
5+
from collections import Counter
6+
7+
8+
class OperatingSystem(Enum):
9+
MACOS = "macOS"
10+
ARCH = "Arch Linux"
11+
UBUNTU = "Ubuntu"
12+
13+
@classmethod
14+
def from_string(cls, s: str):
15+
for os in cls:
16+
if os.value.lower() == s.lower():
17+
return os
18+
raise ValueError(f"Unsupported operating system: {s}")
19+
20+
21+
@dataclass(frozen=True)
22+
class Person:
23+
name: str
24+
age: int
25+
preferred_operating_system: OperatingSystem
26+
27+
28+
@dataclass(frozen=True)
29+
class Laptop:
30+
id: int
31+
manufacturer: str
32+
model: str
33+
screen_size_in_inches: float
34+
operating_system: OperatingSystem
35+
36+
37+
def find_possible_laptops(laptops: List[Laptop], person: Person) -> List[Laptop]:
38+
return [laptop for laptop in laptops if laptop.operating_system == person.preferred_operating_system]
39+
40+
41+
def main():
42+
# Existing inventory
43+
laptops = [
44+
Laptop(id=1, manufacturer="Dell", model="XPS", screen_size_in_inches=13, operating_system=OperatingSystem.ARCH),
45+
Laptop(id=2, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system=OperatingSystem.UBUNTU),
46+
Laptop(id=3, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system=OperatingSystem.UBUNTU),
47+
Laptop(id=4, manufacturer="Apple", model="macBook", screen_size_in_inches=13, operating_system=OperatingSystem.MACOS),
48+
]
49+
50+
# Read and validate user input
51+
try:
52+
name = input("Enter your name: ").strip()
53+
if not name:
54+
raise ValueError("Name cannot be empty.")
55+
56+
age_input = input("Enter your age: ").strip()
57+
age = int(age_input)
58+
if age <= 0:
59+
raise ValueError("Age must be a positive integer.")
60+
61+
print("Available operating systems:")
62+
for os in OperatingSystem:
63+
print(f"- {os.value}")
64+
os_input = input("Enter your preferred operating system: ").strip()
65+
preferred_os = OperatingSystem.from_string(os_input)
66+
67+
except ValueError as e:
68+
print(f"Error: {e}", file=sys.stderr)
69+
sys.exit(1)
70+
71+
# Create the person
72+
person = Person(name=name, age=age, preferred_operating_system=preferred_os)
73+
74+
# Check availability
75+
possible_laptops = find_possible_laptops(laptops, person)
76+
print(f"\nThere are {len(possible_laptops)} laptops available with {preferred_os.value}.")
77+
78+
# Count OS distribution
79+
os_counts = Counter(laptop.operating_system for laptop in laptops)
80+
most_common_os, most_common_count = os_counts.most_common(1)[0]
81+
82+
if most_common_os != preferred_os and most_common_count > len(possible_laptops):
83+
print(f"Note: There are more laptops available with {most_common_os.value} ({most_common_count} total).")
84+
print(f"If you're willing to accept {most_common_os.value}, you're more likely to get a laptop.")
85+
86+
87+
if __name__ == "__main__":
88+
main()

0 commit comments

Comments
 (0)