SimplePrograms challenge

Pete Forman pete.forman at
Wed Jun 20 18:31:29 CEST 2007

Steve Howell <showell30 at> writes:

 >    3) The median example raises the question of what
 > would happen with an empty list.  I don't think it's
 > important to cover that case in the simple example
 > (it's really the responsibility of the caller IMO not
 > to call median with nonsense arguments), but perhaps
 > there is a simple way to fix this?

An empty list raises an IndexError, a non-iterable raises TypeError.
This is correct behavior IMHO, there is nothing to fix.  Median should
return an element (or average of two) from its input and if that is
not meaningful returning None or other special value is neither
appropriate nor Pythonic.  Only the caller knows how to handle such
exceptional circumstances.

Actually there are some other issues.  Should the median of an even
number of integers allow halves?  Should median insist that its input
has an odd number of elements?  But it's tough squeezing all that
discourse into 13 or 14 lines ;-)  BankAccount allows arbitrarily
large withdrawals, is that to be fixed too?
Pete Forman                -./\.-  Disclaimer: This post is originated
WesternGeco                  -./\.-   by myself and does not represent
pete.forman at    -./\.-   the opinion of Schlumberger or           -./\.-   WesternGeco.

More information about the Python-list mailing list