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.as px). 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 1896
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.net wrote:
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.as px). 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...@lexic on.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.as px). 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) for me to tell them each and everything in
python, I am trying to cover as much as I can. Here are the areas which I am
planning to cover :
1. Basic Language facilities : (Data structures, control structures,
input/output, object oriented...
|
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?
2. C use the semicolon as a statement terminator, but also allows a
statement to be empty(that is, consisting of only a semicolon), so that the
following is a legal C program:
main() {
;;;;;;;;;
|
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 know any website
where there are exercises with solutions so I can try to solve them
and can look at the solutions when I'm stucked.I'd like to get
exercises about;
Strings
Arrays
Pointers
|
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 emphasises the programming
logic skills. The examples are just introduction to particular
definition of the concepts.
I would appreciate if any one provide me the resources to find such
exercise(irrespective of Programming language) , working on which...
|
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 skill?
thanks
bobby
| |
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 because the exercises tend
to be overly trivial, but if someone knows of somewhere that I can get some
general ideas for programming exercises then i'd appreciate it.
|
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 explanation of design patterns per se, but am
looking for problems to solve using design patterns so that I can understand
them better.
|
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 challenges/programs to reinforce the
material and improve my skills. I started out with stuff like "Guess my
number" games, hangman, etc. and moved on to making poker and card
games to work with classes. For GUIs I created games like minesweeper,
and a...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |