Task Assignment Implementation in Python
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓Task Assignment runs in O(n log n) time and O(n) space, in Python as in every other language.
- ✓list, enumerate, and sorted(key=...) are the whole toolkit here, no special container is needed.
- ✓Reach for enumerate() plus a key= lambda instead of tracking indices in a parallel list: sorted(enumerate(tasks), key=lambda p: p[1]) keeps each duration bolted to its original index through the sort, and is the idiom Python reviewers expect over building index arrays by hand.
- ✓The Python trap: sorted(enumerate(tasks)) without key= sorts by the whole (index, value) tuple, comparing index first, and since index is already 0..n-1, that sort is a no-op that returns exactly the input order.
- ✓enumerate() itself is a lazy generator, no list behind it. sorted() over it does materialize a real O(n) list of (index, value) tuples, and that list of small heap-allocated tuples is exactly the O(n) space this algorithm spends; sorting the bare durations alone, if indices weren't needed, would be measurably cheaper per element.
What Task Assignment actually is
Task Assignment in Python: what you actually reach for
Python implementation
def task_assignment(k: int, tasks: list[int]) -> list[tuple[int, int]]:
# decorate-sort-undecorate: keep the original index attached to each duration
ranked = sorted(enumerate(tasks), key=lambda pair: pair[1])
pairs: list[tuple[int, int]] = []
lo, hi = 0, len(ranked) - 1
for _ in range(k):
shortest_idx, _ = ranked[lo]
longest_idx, _ = ranked[hi]
pairs.append((shortest_idx, longest_idx))
lo += 1
hi -= 1
return pairsThink Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe Python-specific trap
Performance in Python, beyond the O(n log n)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which Python type or library should I use for Task Assignment?
list, enumerate, and sorted(key=...) are the whole toolkit here, no special container is needed. operator.itemgetter(1) is the idiomatic key= for the enumerate() pairs when you want the extraction done in C rather than through a Python-level lambda.
What is the most common Python mistake when implementing Task Assignment?
sorted(enumerate(tasks)) without key= sorts by the whole (index, value) tuple, comparing index first, and since index is already 0..n-1, that sort is a no-op that returns exactly the input order. No exception, just a pairing that quietly ignores every duration.
How does Task Assignment actually perform in Python?
enumerate() itself is a lazy generator, no list behind it. sorted() over it does materialize a real O(n) list of (index, value) tuples, and that list of small heap-allocated tuples is exactly the O(n) space this algorithm spends; sorting the bare durations alone, if indices weren't needed, would be measurably cheaper per element. Time complexity is O(n log n) and space is O(n).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialBefore paying full price for a tool, check the student deals on AI and developer tools listed on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.