Skip to content

Conversation

@AliQassab
Copy link

@AliQassab AliQassab commented Oct 25, 2025

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

Added solution to allocation laptop and sadness

Questions

Ready to review

@AliQassab AliQassab added 📅 Sprint 4 Assigned during Sprint 4 of this module Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. Module-Decomposition The name of the module. labels Oct 25, 2025
Copy link

@LonMcGregor LonMcGregor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good start on this task - I've left some comments for places where you could think more about the efficiency of the implementation.

sadness = calculate_sadness(person, laptop)
print(f"{person.name} -> {laptop.manufacturer} {laptop.model} ({laptop.operating_system.value}) - Sadness: {sadness}")

print(f"\nTotal Sadness: {calculate_total_sadness(allocation)}")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking about efficiency again, you just looped over everyone to get their individual sadness. The function you're calling here uses a loop as well. Is there any other way of achieving this without needing to loop twice here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, with these changes:

  • Add total_sadness = 0 before the loop
  • Accumulate total_sadness += sadness inside the loop
  • Replace the calculate_total_sadness(allocation) call with total_sadness

# Create a priority queue of (person, laptop, sadness) tuples
# Sort by sadness to allocate best matches first
preferences = []
for person in people:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this approach you are looping over all people and laptops,then afterwards sorting and then doing the assignement.

Is there a way you could achieve this with fewer loops, or not going fully around the loops you have so much?

Imagine if you were doing this for a class full of hundreds of laptops and people - how many times would you end up going around the loops?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For 100 people and 100 laptops:
Before: 10,000 pairs created + sorted
After: At most 10,000 sadness calculations, but typically much fewer due to early exits and shrinking sets

@LonMcGregor LonMcGregor 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. labels Oct 28, 2025
Copy link

@LonMcGregor LonMcGregor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect!

@LonMcGregor LonMcGregor added Complete Volunteer to add when work is complete and all review comments have been addressed. and removed Reviewed Volunteer to add when completing a review with trainee action still to take. labels Oct 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Complete Volunteer to add when work is complete and all review comments have been addressed. Module-Decomposition The name of the module. 📅 Sprint 4 Assigned during Sprint 4 of this module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants