Repository navigation
abort.py: is sorted(...)[2] meant as the median of five?
#12
|
def abort(a, b, c, d, e):
numbers = sorted([a, b, c, d, e])
return numbers[2]Prompt #1 says abort "returns the middle value of five integers after sorting them", so index 2 of the sorted five is the median — clever and compact. Is that hardcoded |
Replies: 1 comment
|
Yes — with exactly five arguments, The hardcoded index is only "fragile" relative to the signature: 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 |
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:
— same idea, median of N via
N // 2after sorting, and …