generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 42
London| SDC-N0v-25| Ikenna Agulobi | Sprint 5| Laptop allocation #313
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
Open
ike-agu
wants to merge
2
commits into
CodeYourFuture:main
Choose a base branch
from
ike-agu:laptop_allocation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| from dataclasses import dataclass | ||
| from enum import Enum | ||
| from typing import List, Dict, Optional | ||
| import sys | ||
|
|
||
| class OperatingSystem(Enum): | ||
| MACOS = "macOS" | ||
| ARCH = "Arch Linux" | ||
| UBUNTU = "Ubuntu" | ||
| @dataclass(frozen=True) | ||
| class Person: | ||
| name: str | ||
| age: int | ||
| preferred_operating_system: List[OperatingSystem] | ||
|
|
||
| # custom hash to hash the immutable version of the list | ||
| def __hash__(self) -> int: | ||
| return hash((self.name, self.age, tuple(self.preferred_operating_system))) | ||
| @dataclass(frozen=True) | ||
| class Laptop: | ||
| id: int | ||
| manufacturer: str | ||
| model: str | ||
| screen_size_in_inches: float | ||
| operating_system: OperatingSystem | ||
|
|
||
| # =====define parse operating system====== | ||
| def parse_operating_system(raw: str) -> OperatingSystem: | ||
| normalized = raw.strip().lower() | ||
|
|
||
| aliases = { | ||
| "macos": OperatingSystem.MACOS, | ||
| "mac": OperatingSystem.MACOS, | ||
| "arch": OperatingSystem.ARCH, | ||
| "arch linux": OperatingSystem.ARCH, | ||
| "ubuntu": OperatingSystem.UBUNTU, | ||
| } | ||
|
|
||
|
|
||
| if normalized not in aliases: | ||
| valid = ", ".join(sorted(aliases.keys())) | ||
| print(f"Error: invalid operating system. Try one of: {valid}", file=sys.stderr) | ||
| sys.exit(1) | ||
|
|
||
| return aliases[normalized] | ||
|
|
||
| # ======= sadness helper ============ | ||
| def sadness(person: Person, laptop: Laptop) -> int: | ||
| laptop_os = laptop.operating_system | ||
| preferences = person.preferred_operating_system | ||
| if laptop_os not in preferences: | ||
| return 100 | ||
| return preferences.index(laptop_os) | ||
|
|
||
| # ======= Laptop allocation ============ | ||
| def allocate_laptops(people: List[Person], laptops: List[Laptop]) -> Dict[Person, Laptop]: | ||
| if len(laptops) < len(people): | ||
| raise ValueError("Not enough laptops to allocate one per person.") | ||
|
|
||
| sorted_people = sorted(people, key=lambda p: len(p.preferred_operating_system)) | ||
|
|
||
| remaining_laptops = laptops[:] | ||
| assignment: Dict[Person, Laptop] = {} | ||
|
|
||
| for person in sorted_people: | ||
| best_laptop:Optional[Laptop] = None | ||
| best_score = 10**9 | ||
|
|
||
| for laptop in remaining_laptops: | ||
| score = sadness(person, laptop) | ||
| if score < best_score: | ||
| best_score = score | ||
| best_laptop = laptop | ||
| if score == 0: | ||
| break | ||
|
|
||
| if best_laptop is None: | ||
| raise RuntimeError("Allocation failed unexpectedly.") | ||
|
|
||
| assignment[person] = best_laptop | ||
| remaining_laptops.remove(best_laptop) | ||
|
|
||
| return assignment | ||
|
|
||
| # ======define main ================ | ||
| def main() -> None: | ||
| laptops = [ | ||
| Laptop(id=1, manufacturer="Dell", model="XPS", screen_size_in_inches=13.0, operating_system=OperatingSystem.ARCH), | ||
| Laptop(id=2, manufacturer="Dell", model="XPS", screen_size_in_inches=15.0, operating_system=OperatingSystem.UBUNTU), | ||
| Laptop(id=3, manufacturer="Dell", model="XPS", screen_size_in_inches=15.0, operating_system=OperatingSystem.UBUNTU), | ||
| Laptop(id=4, manufacturer="Apple", model="macBook", screen_size_in_inches=13.0, operating_system=OperatingSystem.MACOS), | ||
| ] | ||
|
|
||
| people = [ | ||
| Person(name="Imran", age=22, preferred_operating_system=[OperatingSystem.UBUNTU, OperatingSystem.ARCH, OperatingSystem.MACOS]), | ||
| Person(name="Eliza", age=34, preferred_operating_system=[OperatingSystem.ARCH, OperatingSystem.UBUNTU]) | ||
| ] | ||
|
|
||
| allocation = allocate_laptops(people, laptops) | ||
|
|
||
| # Print results | ||
| for person, laptop in allocation.items(): | ||
| print(f"{person.name}: Laptop {laptop.id} ({laptop.operating_system.value}) sadness={sadness(person, laptop)}") | ||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Nice! The non-hashability of List caught many people out (including me, initially - I'm more C# than Python). This is a nice workaround.
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.
@OracPrime Thanks for reviewing my PR and for the feedback. I really appreciate 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.
It was good code - you may notice I took the liberty of quoting it in the Nov SDC slack channel!