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
121 changes: 121 additions & 0 deletions Sprint5-exercise/laptop-allocation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
from enum import Enum
from typing import List, Dict
from dataclasses import dataclass

from itertools import permutations



class OperatingSystem(Enum):
UBUNTU = "ubuntu"
MACOS = "macos"
ARCH = "arch linux"


@dataclass
class Person:
name: str
age: int
preferred_operating_system: List[OperatingSystem]


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


def sadness(person: Person, operating_system: OperatingSystem) -> int:
sad: int = 100
for index, os in enumerate(person.preferred_operating_system):
if os == operating_system:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Did you consider preferred_operating_system.index() ?

sad = index
return sad

return sad


def allocate_laptops(
people: List[Person], laptops: List[Laptop]
) -> Dict[Person, Laptop]:
how_many_people = len(people)
how_many_laptops = len(laptops)
if how_many_laptops < how_many_people:
raise ValueError("Not enough laptops for all people")
matrix = [[0 for i in range(how_many_laptops)] for _ in range(how_many_people)]
for i, person in enumerate(people):
for j, laptop in enumerate(laptops):
matrix[i][j] = sadness(person, laptop.operating_system)

best_perm = None
min_sadness = 100000000000
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

float('inf') is an option

for perm in permutations(range(how_many_laptops)):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

If you've got 15 laptops, how many times do this loop iterate?


total = 0
for i in range(how_many_people):

total += matrix[i][perm[i]]

if total < min_sadness:
min_sadness = total
best_perm = perm
results = {}
for index, person in enumerate(people):
results[person.name] = laptops[best_perm[index]]
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

You've declared the function as a dictionary with a key of Person, but you're using the name as the key. You need to be consistent.


return results


def main() -> None:
people = [
Person(
name="Imran",
age=22,
preferred_operating_system=[
OperatingSystem.UBUNTU,
OperatingSystem.ARCH,
OperatingSystem.MACOS,
],
),
Person(
name="Eliza",
age=34,
preferred_operating_system=[OperatingSystem.UBUNTU, OperatingSystem.ARCH],
),
Person(
name="Ahmad",
age=34,
preferred_operating_system=[OperatingSystem.MACOS],
),
]
laptops = [
Laptop(
id=1,
manufacturer="Dell",
model="XPS",
screen_size_in_inches=13,
operating_system=OperatingSystem.MACOS,
),
Laptop(
id=2,
manufacturer="Dell",
model="XPS",
screen_size_in_inches=15,
operating_system=OperatingSystem.MACOS,
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

A Dell running MACOS? Impressive

),
Laptop(
id=3,
manufacturer="Dell",
model="XPS",
screen_size_in_inches=15,
operating_system=OperatingSystem.UBUNTU,
),
]

print(allocate_laptops(people, laptops))


main()
Loading