Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions laptop-allocation.py

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.

Copy link
Author

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.

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():

Choose a reason for hiding this comment

The 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?

Copy link
Author

Choose a reason for hiding this comment

The 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
Eliza gets Laptop #1 with Arch Linux

This is what I expected.

print(f"{person.name} gets Laptop #{laptop.id} with {laptop.operating_system.value}")