-
-
Notifications
You must be signed in to change notification settings - Fork 5
London | Ameneh Keshavarz | Module_Decomposition | Week 4|implement laptop allocation #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
from dataclasses import dataclass | ||
from enum import Enum | ||
from typing import List, Dict, Tuple, Optional | ||
|
||
|
||
# This is a list of possible operating systems for laptops | ||
class OperatingSystem(Enum): | ||
MACOS = "macOS" | ||
ARCH = "Arch Linux" | ||
UBUNTU = "Ubuntu" | ||
|
||
|
||
# This is a person who wants a laptop | ||
@dataclass(frozen=True) | ||
class Person: | ||
name: str | ||
age: int | ||
# Each person has a list of operating systems they like, ordered by preference | ||
preferred_operating_system: Tuple[OperatingSystem, ...] | ||
|
||
|
||
# This is a laptop with an ID and other details | ||
@dataclass(frozen=True) | ||
class Laptop: | ||
id: int | ||
manufacturer: str | ||
model: str | ||
screen_size_in_inches: float | ||
operating_system: OperatingSystem | ||
|
||
|
||
# This function checks how much a person will dislike a laptop | ||
# Lower number = happier, 100 = very unhappy | ||
def calculate_sadness(person: Person, laptop: Laptop) -> int: | ||
try: | ||
# Find the position of the laptop's OS in the person's preference list | ||
return person.preferred_operating_system.index(laptop.operating_system) | ||
except ValueError: | ||
# If the OS is not in the list, return 100 (very sad) | ||
return 100 | ||
|
||
|
||
# This function gives laptops to people based on what makes them happiest | ||
def allocate_laptops(people: List[Person], laptops: List[Laptop]) -> Dict[Person, Laptop]: | ||
allocation: Dict[Person, Laptop] = {} | ||
# Make a copy of the laptop list so we can remove laptops when they are taken | ||
available_laptops = laptops.copy() | ||
|
||
# Go through each person | ||
for person in people: | ||
best_laptop: Optional[Laptop] = None | ||
lowest_sadness = float('inf') # Start with a very high sadness | ||
|
||
# Check each available laptop to find the best match | ||
for laptop in available_laptops: | ||
sadness = calculate_sadness(person, laptop) | ||
# If this laptop makes the person less sad than others, remember it | ||
if sadness < lowest_sadness: | ||
lowest_sadness = sadness | ||
best_laptop = laptop | ||
|
||
# If we found a good laptop, give it to the person and remove it from the list | ||
if best_laptop: | ||
allocation[person] = best_laptop | ||
available_laptops.remove(best_laptop) | ||
|
||
return allocation | ||
|
||
|
||
# Test the program with some people and laptops | ||
if __name__ == "__main__": | ||
people = [ | ||
Person("Imran", 22, (OperatingSystem.UBUNTU, OperatingSystem.ARCH)), | ||
Person("Eliza", 34, (OperatingSystem.ARCH, OperatingSystem.MACOS)) | ||
] | ||
|
||
laptops = [ | ||
Laptop(1, "Dell", "XPS", 13, OperatingSystem.ARCH), | ||
Laptop(2, "Dell", "XPS", 15, OperatingSystem.UBUNTU), | ||
Laptop(3, "Apple", "MacBook", 13, OperatingSystem.MACOS) | ||
] | ||
|
||
allocation = allocate_laptops(people, laptops) | ||
|
||
# Print the result | ||
for person, laptop in allocation.items(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What output do you expect here? Does it match the output you get after running your code? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought the laptops would be given to people based on what operating system they like the most. Imran likes Ubuntu more than Arch, so I expected him to get the Ubuntu laptop (Laptop #2). Eliza likes Arch more than macOS, so I expected her to get the Arch laptop (Laptop #1). When I ran the code, the result was: Imran gets Laptop #2 with Ubuntu This is what I expected. |
||
print(f"{person.name} gets Laptop #{laptop.id} with {laptop.operating_system.value}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note - It is always helpful to add comments to explain the logic of your code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, thanks for the feedback, I added some explantion about the code's logic.