SimplePrograms challenge

Pete Forman pete.forman at westerngeco.com
Wed Jun 20 12:31:29 EDT 2007


Steve Howell <showell30 at yahoo.com> 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 westerngeco.com    -./\.-   the opinion of Schlumberger or
http://petef.port5.com           -./\.-   WesternGeco.



More information about the Python-list mailing list