-
-
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
base: main
Are you sure you want to change the base?
Conversation
OracPrime
left a comment
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.
The code is generally very good and has some nice touches in terms of normalization etc. However your algorithm doesn't scale. Can you think of a "good enough" rather than perfect one that does scale?
| @@ -0,0 +1,104 @@ | |||
| from dataclasses import dataclass | |||
| from enum import Enum | |||
| from typing import List, Dict, Optional, Tuple | |||
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.
I suspect you started using a Tuple and changed it to a List? It's good practice to remove unwanted imports.
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.
Thanks for pointing that out. I've removed unwanted imports.
|
|
||
| # =====define parse operating system====== | ||
| def parse_operating_system(raw: str) -> OperatingSystem: | ||
| normalized = raw.strip().lower() |
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.
Good!
|
|
||
| if normalized not in aliases: | ||
| valid = ", ".join(sorted(aliases.keys())) | ||
| print(f"Error: invalid operating system. Try one of: {valid}", file=sys.stderr) |
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.
Like the inclusion of allowed values in the error message. Like the fact that you're using stderr
| best_assignment: Optional[Dict[Person, Laptop]] = None | ||
| best_total_sadness: Optional[int] = None | ||
|
|
||
| for chosen_laptops in itertools.combinations(laptops, len(people)): |
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 will work for 5 laptops. What happens if you've got 20 laptops?
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.
Hi @OracPrime , you’re absolutely right. The original brute-force approach I implemented only works for very small number of laptops as you pointed out, because it explores all possible permutations and does not scale well.
I’ve refactored my code to use a greedy approach, which allows it to scale to much larger inputs (for example, 20 or more laptops). This approach doesn’t always find the perfect solution, but it gives a reasonable result and runs efficiently even as the number of laptops increases.
Thanks for taking the time to review my code and for your helpful feedback.
…Remove unwanted imports
|
@OracPrime, Thanks for taking the time to review my code and for your feedback. I’ve implemented the changes you pointed out. When you’re available, could you please take another look? |
OracPrime
left a comment
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.
Good work!
|
|
||
| # 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))) |
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!
Learners, PR Template
Self checklist
Changelist
This PR contains the Laptop allocation exercise of sprint 5