-
-
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
base: main
Are you sure you want to change the base?
Changes from all 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 |
|---|---|---|
| @@ -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: | ||
| 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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]] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
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.
Did you consider preferred_operating_system.index() ?