15 Exercises to Know A Programming Language

Martin martin.clausen at gmail.com
Wed Jul 4 16:37:24 EDT 2007


On Jul 3, 1:47 pm, John Machin <sjmac... at lexicon.net> wrote:
> On Jul 3, 7:58 pm, Martin <martin.clau... at gmail.com> wrote:
>
>
>
> > I am trying to improve my Python skills through some exercises.
> > Currently I am working on Larry's "15 exercises to know a programming
> > language " (http://www.knowing.net/
> > PermaLink,guid,f3b9ba36-848e-43f8-9caa-232ec216192d.aspx). The first
> > exercise is this:
>
> > "Write a program that takes as its first argument one of the words
> > 'sum,' 'product,' 'mean,' or 'sqrt'  and for further arguments a
> > series of numbers. The program applies the appropriate function to
> > the series."
>
> > My solution so far is this:
>
> >http://dpaste.com/13469/
>
> > I would really like some feedback. Is this a good solution? is it
> > efficient? robust? what could be improved? any not looking for a
> > revised solution, hints on what to improve are also very welcome.
>
> > Martin
>
> sum is a builtin function in Python 2.3 and later. You could do
> something like this:
>
> try:
>     sum
> except NameError:
>     def sum(args):
>         return reduce(operator.add, args)
>
> Tested with 2.5 back to 2.1, and 1.5.2 :-)

Thanks John and Daniel. My revised version now uses sum(). I have
started to implement some basic error checking and validation.

Martin




More information about the Python-list mailing list