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 5 1879
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
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
"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
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 :-)
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: vivek |
last post by:
Hi all,
Here we are conducting a QIP program for teachers in other colleges. I am
assigned the job of taking lectures in Python. Although there will be not much
time (exactly speaking : 4 hours)...
|
by: feivue |
last post by:
1. In Java, intergers can be assigned to real variales, but not vice versa.
What design principle does this violate? In C, this restriction does not
exist. What design principle does this violate?...
|
by: Wous Mant |
last post by:
Hello,
I am a student in an university and studying C++.Final exams are
coming.I'd like to solve as much exercises as possible.I'd like you to
test myself with different kind of exercises.Do you...
|
by: Kanthi Kiran Narisetti |
last post by:
Hi ALL,
I am new to programming and python. In my quest to get good at
programming, even when I am able to get through the python tutorials I
feel for lack of exercises & examples that...
|
by: Robert S |
last post by:
hello folks,
i am an intermediate or low intermediate level C programer.does anybody know
a good website that offers projects,exercises where i can write some codes
and improve my programming...
|
by: Zachary Turner |
last post by:
I am just learning the syntax of C#, and I want some "exercise" type ideas
to allow me to test various features of the language. I don't really want
to resort to a "Teach Yourself C#" type of book...
|
by: EnterTheName |
last post by:
Hello Everyone,
Does anyone know of a C# design patterns book which has good programming
exercises/problems at the end of each chapter(like a workbook of some sort).
I am not looking for an...
|
by: jopy887 |
last post by:
Hey people,
Does someone know a good site for C exercises?
For beginners..
Jonathan
|
by: btkuhn |
last post by:
Hi guys,
I'm learning Python by teaching myself, and after going through several
tutorials I feel like I've learned the basics. Since I'm not taking a
class or anything, I've been doing...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |