-
-
Notifications
You must be signed in to change notification settings - Fork 88
NW | 26-SDC-Mar | Ahmad Hmedan | Sprint 5 | Implement laptop allocation #508
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
AhmadHmedann
wants to merge
4
commits into
CodeYourFuture:main
Choose a base branch
from
AhmadHmedann:Implement-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
4 commits
Select commit
Hold shift + click to select a range
4afdc9f
feat: implement laptop allocation using permutation and sadness matrix
AhmadHmedann 532fc1b
reformat
AhmadHmedann 9990844
Use Hungarian algorthms and avoids the unhashable List problem
AhmadHmedann 981f35c
use preferred_operating_system.index()
AhmadHmedann 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,107 @@ | ||
| from enum import Enum | ||
| from typing import Dict | ||
| from dataclasses import dataclass | ||
| from scipy.optimize import linear_sum_assignment | ||
|
|
||
|
|
||
| class OperatingSystem(Enum): | ||
| UBUNTU = "ubuntu" | ||
| MACOS = "macos" | ||
| ARCH = "arch linux" | ||
|
|
||
|
|
||
| @dataclass (frozen=True) | ||
| class Person: | ||
| name: str | ||
| age: int | ||
| preferred_operating_system: tuple[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: | ||
| try: | ||
| return person.preferred_operating_system.index(operating_system) | ||
| except ValueError: | ||
| return 100 | ||
|
|
||
| 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") | ||
| sadness_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): | ||
| sadness_matrix[i][j] = sadness(person, laptop.operating_system) | ||
| # print(sadness_matrix); | ||
| row_indexes, column_indexes = linear_sum_assignment(sadness_matrix) | ||
| result = {} | ||
|
|
||
| for row, column in zip(row_indexes, column_indexes): | ||
| person = people[row] | ||
| laptop = laptops[column] | ||
| result[person] = laptop | ||
|
|
||
| return result | ||
|
|
||
| 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,), | ||
| ), | ||
| Person( | ||
| name="Ahmad", | ||
| age=34, | ||
| preferred_operating_system=(OperatingSystem.UBUNTU, OperatingSystem.MACOS,), | ||
| ), | ||
| ] | ||
| laptops = [ | ||
| Laptop( | ||
| id=1, | ||
| manufacturer="Dell", | ||
| model="XPS", | ||
| screen_size_in_inches=13, | ||
| operating_system=OperatingSystem.ARCH | ||
| ), | ||
| Laptop( | ||
| id=2, | ||
| manufacturer="HM", | ||
| model="XPS", | ||
| screen_size_in_inches=15, | ||
| operating_system=OperatingSystem.MACOS, | ||
| ), | ||
| Laptop( | ||
| id=3, | ||
| manufacturer="Dell", | ||
| model="XPS", | ||
| screen_size_in_inches=15, | ||
| operating_system=OperatingSystem.UBUNTU, | ||
| ), | ||
| ] | ||
|
|
||
| print(allocate_laptops(people, laptops)) | ||
|
|
||
|
|
||
| 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.
A Dell running MACOS? Impressive