generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 17
Glasgow | Sheetal Kharab | Module-Decomposition | Sprint 4 | Implement laptop allocation #29
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
sheetalkharab
wants to merge
2
commits into
CodeYourFuture:main
Choose a base branch
from
sheetalkharab:feature/allocate-laptops
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 @@ | ||
| node_modules/ |
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,88 @@ | ||
| from dataclasses import dataclass | ||
| from enum import Enum | ||
| from typing import Dict, List | ||
|
|
||
| class OperatingSystem(Enum): | ||
| MACOS = "macOS" | ||
| ARCH = "Arch Linux" | ||
| UBUNTU = "Ubuntu" | ||
|
|
||
| @dataclass(frozen=True) | ||
| class Person: | ||
| name: str | ||
| age: int | ||
| # Sorted in order of preference, most preferred is first. | ||
| preferred_operating_system: List[OperatingSystem] | ||
|
|
||
| def __hash__(self): | ||
| 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 | ||
|
|
||
|
|
||
| def allocate_laptops(people: List[Person], laptops: List[Laptop]) -> Dict[Person, Laptop]: | ||
| allocations: Dict[Person, Laptop] = {} | ||
| available_laptops = laptops.copy() | ||
|
|
||
| for person in people: | ||
| # Find laptop that gives this person minimum sadness | ||
| if available_laptops: | ||
| best_laptop = min(available_laptops, key=lambda l: calculate_sadness(person, l)) | ||
| allocations[person] = best_laptop | ||
| available_laptops.remove(best_laptop) # Laptop now allocated | ||
| else: | ||
| # No laptops left, assign None or a “dummy” Laptop with sadness 100 | ||
| allocations[person] = None | ||
|
|
||
| return allocations | ||
|
|
||
|
|
||
| def calculate_sadness(person: Person, laptop: Laptop) -> int: | ||
| """Return sadness for a person if given this laptop.""" | ||
| if laptop is None: | ||
| return 100 # no laptop, maximum sadness | ||
| try: | ||
| return person.preferred_operating_system.index(laptop.operating_system) | ||
| except ValueError: | ||
| return 100 # OS not in preferences | ||
|
|
||
|
|
||
| # Sample laptops | ||
| laptops = [ | ||
| Laptop(id=1, manufacturer="Dell", model="XPS", screen_size_in_inches=13, operating_system=OperatingSystem.ARCH), | ||
| Laptop(id=2, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system=OperatingSystem.UBUNTU), | ||
| Laptop(id=3, manufacturer="Apple", model="MacBook", screen_size_in_inches=13, operating_system=OperatingSystem.MACOS), | ||
| ] | ||
|
|
||
| # Sample people with preferences | ||
| people = [ | ||
| Person(name="Alice", age=25, preferred_operating_system=[OperatingSystem.UBUNTU, OperatingSystem.MACOS]), | ||
| Person(name="Bob", age=30, preferred_operating_system=[OperatingSystem.MACOS, OperatingSystem.ARCH]), | ||
| Person(name="Charlie", age=22, preferred_operating_system=[OperatingSystem.ARCH, OperatingSystem.UBUNTU]), | ||
| Person(name="Jack", age=18, preferred_operating_system=[OperatingSystem.ARCH, OperatingSystem.UBUNTU]), | ||
| Person(name="Faith", age=23, preferred_operating_system=[OperatingSystem.ARCH, OperatingSystem.UBUNTU]), | ||
| ] | ||
|
|
||
| # Allocate laptops | ||
| allocations = allocate_laptops(people, laptops) | ||
|
|
||
| # Calculate total sadness | ||
| total_sadness = sum(calculate_sadness(person, laptop) for person, laptop in allocations.items()) | ||
| print(f"\nTotal sadness: {total_sadness}") | ||
|
|
||
| # Print allocations and sadness | ||
| for person, laptop in allocations.items(): | ||
| if laptop is not None: | ||
| print(f"{person.name} got {laptop.manufacturer} {laptop.model} ({laptop.operating_system.value}) " | ||
| f"→ Sadness: {calculate_sadness(person, laptop)}") | ||
| else: | ||
| print(f"{person.name} got no laptop → Sadness: 100") | ||
|
|
||
Oops, something went wrong.
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.
This method works, one thing to be careful of is this think how often this function will run.
It will always run, for every laptop, even if the very first one were a perfect match.
It's not an issue in your example here, but imagine if we were running this with thousands of laptop and people entries