-
-
Notifications
You must be signed in to change notification settings - Fork 42
Manchester | 25-SDC-Nov | Rahwa Haile | Sprint 5 |implement laptop allocation #292
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 2 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 |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| node_modules | ||
| .venv | ||
| 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): | ||
|
||
| 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), | ||
|
||
| 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) | ||
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.
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.
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.
Thank you @OracPrime . Agreed — I’ll generate and commit a requirements.txt so the environment can be reproduced