Skip to content
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
.venv

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While it's correct to gitignore .venv, if you've had to create a venv you should probably commit a requirements.txt that describes it.

88 changes: 88 additions & 0 deletions sprint-5/laptop-allocation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
from dataclasses import dataclass
from enum import Enum
from typing import List, Dict
from itertools import permutations

unpreferred_OS_penalty = 100

class OperatingSystem(Enum):
MACOS = "macOS"
ARCH = "Arch Linux"
UBUNTU = "Ubuntu"

@dataclass(frozen=True)
class Person:
name: str
age: int
preferred_operating_system: tuple

@dataclass(frozen=True)
class Laptop:
id: int
manufacturer: str
model: str
screen_size_in_inches: float
operating_system: OperatingSystem

def allocate_laptops(people: List[Person], laptops: List[Laptop]) -> Dict[Person, Laptop]:
if len(people) != len(laptops):
raise ValueError("Number of people must match number of laptops")

best_assignment = None
lowest_sadness = float("inf")

for perm in permutations(laptops):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you need to revisit this algorithm. If you have 20 laptops, how many iterations is this loop?

total_sadness = 0
for person, laptop in zip(people, perm):
if laptop.operating_system in person.preferred_operating_system:
sadness = person.preferred_operating_system.index(laptop.operating_system)
else:
sadness = unpreferred_OS_penalty
total_sadness += sadness

if total_sadness < lowest_sadness:
lowest_sadness = total_sadness
best_assignment = perm

return {person: laptop for person, laptop in zip(people, best_assignment)}

laptops = [
Laptop(1, "Dell", "XPS 13", 13, OperatingSystem.ARCH),
Laptop(2, "HP", "Spectre 15", 15, OperatingSystem.UBUNTU),
Laptop(3, "Lenovo", "ThinkPad 14", 14, OperatingSystem.UBUNTU),
Laptop(4, "Apple", "MacBook Air", 13, OperatingSystem.MACOS),
Laptop(5, "Apple", "MacBook Pro", 16, OperatingSystem.MACOS),
Laptop(6, "Dell", "Latitude", 15, OperatingSystem.ARCH),
Laptop(7, "HP", "EliteBook", 13, OperatingSystem.MACOS),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HP running MacOS? Cute :)

Laptop(8, "Lenovo", "Yoga", 14, OperatingSystem.UBUNTU)
]

people = [
Person("Alice", 29, (OperatingSystem.UBUNTU, OperatingSystem.MACOS)),
Person("Bob", 34, (OperatingSystem.ARCH, OperatingSystem.UBUNTU)),
Person("Charlie", 40, (OperatingSystem.MACOS, OperatingSystem.ARCH)),
Person("Diana", 25, (OperatingSystem.MACOS,)),
Person("Ethan", 31, (OperatingSystem.UBUNTU, OperatingSystem.ARCH)),
Person("Fiona", 27, (OperatingSystem.MACOS, OperatingSystem.UBUNTU)),
Person("George", 22, (OperatingSystem.ARCH, OperatingSystem.MACOS)),
Person("Zara", 33, (OperatingSystem.ARCH, OperatingSystem.MACOS))
]

assignment = allocate_laptops(people, laptops)


for person, laptop in assignment.items():
person_sadness_score = (
person.preferred_operating_system.index(laptop.operating_system)
if laptop.operating_system in person.preferred_operating_system
else unpreferred_OS_penalty
)
print(f"{person.name} was allocated {laptop.manufacturer} {laptop.model} "
f"with {laptop.operating_system.value} (Score: {person_sadness_score})")

total_sadness = sum(
person.preferred_operating_system.index(laptop.operating_system)
if laptop.operating_system in person.preferred_operating_system else unpreferred_OS_penalty
for person, laptop in assignment.items()
)
print("\nTotal sadness:", total_sadness)