Skip to content
Discussion options

You must be logged in to vote

Yes — with exactly five arguments, sorted(...)[2] is the median, so the compact form is correct against prompt #1 as written. Index 2 of a sorted 5-tuple is definitionally the middle value; there's no off-by-one.

The hardcoded index is only "fragile" relative to the signature: abort(a, b, c, d, e) is fixed at five, so the [2] and the signature are consistent — I'd leave it as the intended definition and treat any signature change as out of scope for this exercise.

If you ever wanted the general form (partly because the piscine-style prompts encourage seeing the pattern), it's:

def abort(*nums):
    return sorted(nums)[len(nums) // 2]

— same idea, median of N via N // 2 after sorting, and …

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by dab00gieman
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants