471,310 Members | 1,615 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,310 software developers and data experts.

15 Exercises to Know A Programming Language

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

Jul 3 '07 #1
5 1775
On Tue, 03 Jul 2007 09:58:16 +0000, Martin wrote:
"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.
Don't use `eval()` if it is not absolutely necessary. Especially if the
input comes from a user it's a security hole. `float()` is the function
to use here.

`mean()` does not work as you try to divide a list by a number.

Ciao,
Marc 'BlackJack' Rintsch
Jul 3 '07 #2
On Jul 3, 12:25 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
On Tue, 03 Jul 2007 09:58:16 +0000, Martin wrote:
"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.

Don't use `eval()` if it is not absolutely necessary. Especially if the
input comes from a user it's a security hole. `float()` is the function
to use here.

`mean()` does not work as you try to divide a list by a number.

Ciao,
Marc 'BlackJack' Rintsch
Thanks for the feedback. I have posted a revised version here (http://
dpaste.com/13474/) where mean works. The reason I use eval is I want
it to work for complex numbers too, but I guess i could check for the
presence of a "j" in the arguments instead.

Martin

Jul 3 '07 #3
"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.
Don't use `eval()` if it is not absolutely necessary. Especially if the
input comes from a user it's a security hole. `float()` is the function
to use here.

`mean()` does not work as you try to divide a list by a number.

Thanks for the feedback. I have posted a revised version here (http://
dpaste.com/13474/) where mean works. The reason I use eval is I want
it to work for complex numbers too, but I guess i could check for the
presence of a "j" in the arguments instead.

Hi, if I invoke your program without arguments an uncaught exception
is raised. Wouldn't it be better to inform the user that an argument
is expected?

See http://docs.python.org/tut/node10.html
Jul 3 '07 #4
On Jul 3, 7:58 pm, Martin <martin.clau...@gmail.comwrote:
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 :-)

Jul 3 '07 #5
On Jul 3, 1:47 pm, John Machin <sjmac...@lexicon.netwrote:
On Jul 3, 7:58 pm, Martin <martin.clau...@gmail.comwrote:
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

Jul 4 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by vivek | last post: by
9 posts views Thread by feivue | last post: by
8 posts views Thread by Wous Mant | last post: by
4 posts views Thread by Kanthi Kiran Narisetti | last post: by
2 posts views Thread by Robert S | last post: by
1 post views Thread by Zachary Turner | last post: by
reply views Thread by EnterTheName | last post: by
8 posts views Thread by jopy887 | last post: by
13 posts views Thread by btkuhn | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.