Skip to content

Conversation

@zohrehKazemianpour
Copy link

Learners, PR Template

Self checklist

  • I have titled my PR with Region | Cohort | FirstName LastName | Sprint | Assignment Title
  • My changes meet the requirements of the task
  • I have tested my changes
  • My changes follow the style guide

Changelist

This PR implements laptop allocation.

@zohrehKazemianpour zohrehKazemianpour added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Jan 4, 2026
@OracPrime OracPrime added the Review in progress This review is currently being reviewed. This label will be replaced by "Reviewed" soon. label Jan 11, 2026
Copy link

@OracPrime OracPrime left a 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

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?

Copy link
Author

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

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?

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!

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))

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

Copy link
Author

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()

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

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

Copy link
Author

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)

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?

Copy link
Author

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.

@OracPrime OracPrime added Reviewed Volunteer to add when completing a review with trainee action still to take. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. Review in progress This review is currently being reviewed. This label will be replaced by "Reviewed" soon. labels Jan 11, 2026
@zohrehKazemianpour
Copy link
Author

Some intelligent coding here, nice!

Couple of questions to clear up, but overall good work.

Thank you for the detailed review and the kind words! I really appreciate the feedback!

@zohrehKazemianpour zohrehKazemianpour added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Jan 16, 2026
Copy link

@OracPrime OracPrime left a 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

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

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

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. Reviewed Volunteer to add when completing a review with trainee action still to take.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants