-
-
Notifications
You must be signed in to change notification settings - Fork 42
London | 25-SDC-Nov | Zohreh Kazemianpour | Sprint 5 | Laptop-allocation #314
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?
London | 25-SDC-Nov | Zohreh Kazemianpour | Sprint 5 | Laptop-allocation #314
Conversation
…dness scores and improve overall allocation quality
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.
Some intelligent coding here, nice!
Couple of questions to clear up, but overall good work.
| name: str | ||
| age: int | ||
| # Sorted in order of preference, most preferred is first. | ||
| preferred_operating_system: 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.
Why a tuple rather than a list?
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 used a tuple because Person needs to be hashable to serve as a dictionary key. If I used a list, Python would raise a TypeError: unhashable type: list
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.
My version of python doesn't complain. What Python version are you using?
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.
Ah, OK, I now understand it. The data being passed in is Tuples, so when I have preferred_operating_system as a list, Python ignores the type hint and stores the Tuple instead. I'm too used to strongly typed languages!
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.
Take a gold star. Your Python knowledge exceeds mine in this respect (and probably many others, I'm guessing)
| raise ValueError("Not enough laptops to allocate one per person") | ||
|
|
||
|
|
||
| people_sorted = sorted(people, key = lambda p: len(p.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 touch - haven't seen anyone else do this, but it is a clever tweak to the algorithm
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.
Thank you! I looked into more complex approaches like the Hungarian Algorithm, but found it a bit overwhelming at this stage for me. I decided to stick with greedy approach to ensure the pickest user get a compatible laptop before the more flexible users. With this we can minimise the overall sadness score for the group.
| people_sorted = sorted(people, key = lambda p: len(p.preferred_operating_system)) | ||
|
|
||
| result={} | ||
| available_laptops = laptops.copy() |
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 spot that you need the copy if you don't want to change the original
| sadness = calculate_sadness(person, laptop) | ||
| if sadness < smallest_sadness: | ||
| smallest_sadness = sadness | ||
| best_laptop = laptop |
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.
You could of course break out of the loop if sadness is 0
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.
That is a very good point, thanks for mentioning it. I've added a break when sadness == 0 to avoid unnecessary iterations.
|
|
||
| return result | ||
|
|
||
| allocate_laptops(people, 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.
Why is this first call to allocate laptops here?
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 removed the first call. it was a leftover from debugging. thanks for catching that.
Thank you for the detailed review and the kind words! I really appreciate the feedback! |
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.
Thank you for educating me!
| name: str | ||
| age: int | ||
| # Sorted in order of preference, most preferred is first. | ||
| preferred_operating_system: 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.
My version of python doesn't complain. What Python version are you using?
| name: str | ||
| age: int | ||
| # Sorted in order of preference, most preferred is first. | ||
| preferred_operating_system: 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.
Ah, OK, I now understand it. The data being passed in is Tuples, so when I have preferred_operating_system as a list, Python ignores the type hint and stores the Tuple instead. I'm too used to strongly typed languages!
| name: str | ||
| age: int | ||
| # Sorted in order of preference, most preferred is first. | ||
| preferred_operating_system: 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.
Take a gold star. Your Python knowledge exceeds mine in this respect (and probably many others, I'm guessing)
Learners, PR Template
Self checklist
Changelist
This PR implements laptop allocation.