Skip to content

Commit c1c307c

Browse files
committed
sprint 4 prep exercises
1 parent 16ace89 commit c1c307c

2 files changed

Lines changed: 126 additions & 0 deletions

File tree

prep_exercises_sprint_4/enums.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
from enum import Enum
2+
import sys
3+
4+
# Define the possible operating systems as an enum
5+
class OperatingSystem(Enum):
6+
MACOS = "MacOS"
7+
ARCH = "Arch"
8+
UBUNTU = "Ubuntu"
9+
10+
# List of laptops available in the library
11+
laptops = [
12+
{"id": 1, "manufacturer": "Dell", "model": "XPS", "os": OperatingSystem.ARCH},
13+
{"id": 2, "manufacturer": "Dell", "model": "XPS", "os": OperatingSystem.UBUNTU},
14+
{"id": 3, "manufacturer": "Dell", "model": "XPS", "os": OperatingSystem.UBUNTU},
15+
{"id": 4, "manufacturer": "Apple", "model": "MacBook", "os": OperatingSystem.MACOS},
16+
{"id": 5, "manufacturer": "Dell", "model": "Inspiron", "os": OperatingSystem.UBUNTU},
17+
]
18+
19+
def get_user_preferences():
20+
"""Get the user's name and preferred operating system"""
21+
name = input("Enter your name: ").strip()
22+
if not name:
23+
print("Error: Name cannot be empty", file=sys.stderr)
24+
sys.exit(1)
25+
26+
print("\nAvailable operating systems:")
27+
print("• MacOS")
28+
print("• Arch")
29+
print("• Ubuntu")
30+
31+
os_choice = input("Enter your preferred operating system: ").strip().lower()
32+
33+
# Convert string to OperatingSystem enum
34+
if os_choice == "macos":
35+
return name, OperatingSystem.MACOS
36+
elif os_choice == "arch":
37+
return name, OperatingSystem.ARCH
38+
elif os_choice == "ubuntu":
39+
return name, OperatingSystem.UBUNTU
40+
else:
41+
print(f"Error: '{os_choice}' is not a valid operating system", file=sys.stderr)
42+
print("Please choose from: macos, arch, ubuntu", file=sys.stderr)
43+
sys.exit(1)
44+
45+
def count_laptops_by_os():
46+
"""Count how many laptops we have for each operating system"""
47+
counts = {}
48+
for os in OperatingSystem:
49+
counts[os] = 0
50+
51+
for laptop in laptops:
52+
counts[laptop["os"]] += 1
53+
54+
return counts
55+
56+
def main():
57+
print("=== Library Laptop Finder ===")
58+
59+
# Get user input
60+
name, preferred_os = get_user_preferences()
61+
62+
# Count laptops for each OS
63+
os_counts = count_laptops_by_os()
64+
65+
# Show results to user
66+
user_laptop_count = os_counts[preferred_os]
67+
68+
print(f"\nHello {name}!")
69+
print(f"We have {user_laptop_count} laptop(s) with {preferred_os.value}.")
70+
71+
# Check if other OS have more laptops
72+
other_options = []
73+
for os, count in os_counts.items():
74+
if os != preferred_os and count > user_laptop_count:
75+
other_options.append((os, count))
76+
77+
if other_options:
78+
print("\nTip: These operating systems have more laptops available:")
79+
for os, count in other_options:
80+
print(f" • {os.value}: {count} laptops")
81+
82+
# Show complete availability
83+
print(f"\nAll available laptops:")
84+
for os in OperatingSystem:
85+
count = os_counts[os]
86+
print(f" • {os.value}: {count} laptop(s)")
87+
88+
if __name__ == "__main__":
89+
main()
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Parent:
2+
def __init__(self, first_name: str, last_name: str):
3+
self.first_name = first_name
4+
self.last_name = last_name
5+
6+
def get_name(self) -> str:
7+
return f"{self.first_name} {self.last_name}"
8+
9+
10+
class Child(Parent):
11+
def __init__(self, first_name: str, last_name: str):
12+
super().__init__(first_name, last_name)
13+
self.previous_last_names = []
14+
15+
def change_last_name(self, last_name) -> None:
16+
self.previous_last_names.append(self.last_name)
17+
self.last_name = last_name
18+
19+
def get_full_name(self) -> str:
20+
suffix = ""
21+
if len(self.previous_last_names) > 0:
22+
suffix = f" (née {self.previous_last_names[0]})"
23+
return f"{self.first_name} {self.last_name}{suffix}"
24+
25+
person1 = Child("Elizaveta", "Alekseeva")
26+
print(person1.get_name())
27+
print(person1.get_full_name())
28+
person1.change_last_name("Tyurina")
29+
print(person1.get_name())
30+
print(person1.get_full_name())
31+
32+
person2 = Parent("Elizaveta", "Alekseeva")
33+
print(person2.get_name())
34+
# print(person2.get_full_name())
35+
# person2.change_last_name("Tyurina")
36+
print(person2.get_name())
37+
# print(person2.get_full_name())

0 commit comments

Comments
 (0)