Re: Little novice program written in Python

2008-04-28 Thread Rogério Brito
On 04/25/2008 09:30 AM, Nick Craig-Wood wrote: When you are up to speed in python I suggest you check out gmpy for number theory algorithms. Thanks. That is quite useful to know when I don't want to code explicitly the details of the algorithm. Thanks, Rogério. -- Rogério Brito : [EMAIL PR

Re: Little novice program written in Python

2008-04-28 Thread Rogério Brito
On 04/25/2008 05:00 AM, John Machin wrote: On Apr 25, 5:44 pm, Robert Bossy <[EMAIL PROTECTED]> wrote: If the OP insists in not examining a[0] and a[1], this will do exactly the same as the while version: for p in a[2:]: if p: print p ... at the cost of almost doubling the amount

Re: Little novice program written in Python

2008-04-28 Thread Rogério Brito
On 04/25/2008 01:30 AM, Steve Holden wrote: Rogério Brito wrote: I'm just getting my feet wet on Python and, just for starters, I'm coding some elementary number theory algorithms (yes, I know that most of them are already implemented as modules, but this is an exercise in learning the languag

Re: Little novice program written in Python

2008-04-28 Thread Rogério Brito
On 04/25/2008 01:09 AM, Dennis Lee Bieber wrote: On Thu, 24 Apr 2008 21:31:15 -0300, Rogério Brito <[EMAIL PROTECTED]> declaimed the following in comp.lang.python: a = [i for i in range(0,n+1)] Uhm... At least in 2.4 and earlier, range() returns a list... No need for the list-comp in t

Re: Little novice program written in Python

2008-04-25 Thread Robert Bossy
Marc 'BlackJack' Rintsch wrote: Indeed. Would it be a sensible proposal that sequence slices should return an iterator instead of a list? I don't think so as that would break tons of code that relies on the current behavior. Take a look at `itertools.islice()` if you want/need an iterator

Re: Little novice program written in Python

2008-04-25 Thread Max M
hellt skrev: >> Most code is not like that so perhaps you should try something more >> "usual" like sending email, fetching webpages etc. to get a feel for the >> language. >> > em, i would say, that python (esp. with NumPy+Psyco) is very popular > in numerical processing also. I know, and I migh

Re: Little novice program written in Python

2008-04-25 Thread Nick Craig-Wood
Rogério Brito <[EMAIL PROTECTED]> wrote: > I'm just getting my feet wet on Python and, just for starters, I'm coding > some > elementary number theory algorithms (yes, I know that most of them are > already > implemented as modules, but this is an exercise in learning the > language idioms)

Re: Little novice program written in Python

2008-04-25 Thread hellt
On 25 апр, 15:02, Max M <[EMAIL PROTECTED]> wrote: > Rogério Brito skrev: > > > Hi, All. > > > What I would like is to receive some criticism to my code to make it > > more Python'esque and, possibly, use the resources of the computer in a > > more efficient way (the algorithm implemented below is

Re: Little novice program written in Python

2008-04-25 Thread Max M
Rogério Brito skrev: Hi, All. What I would like is to receive some criticism to my code to make it more Python'esque and, possibly, use the resources of the computer in a more efficient way (the algorithm implemented below is the Sieve of Eratosthenes): I agree with the rest here. Your cod

Re: Little novice program written in Python

2008-04-25 Thread Marc 'BlackJack' Rintsch
On Fri, 25 Apr 2008 10:24:16 +0200, Robert Bossy wrote: > John Machin wrote: >> On Apr 25, 5:44 pm, Robert Bossy <[EMAIL PROTECTED]> wrote: >> >>> Peter Otten wrote: >>> If the OP insists in not examining a[0] and a[1], this will do exactly >>> the same as the while version: >>> >>> for p in a[

Re: Little novice program written in Python

2008-04-25 Thread hellt
On 25 апр, 13:29, Arnaud Delobelle <[EMAIL PROTECTED]> wrote: > hellt <[EMAIL PROTECTED]> writes: > > my variant of the sieve > > Since you posted it, you are also looking for advice to improve your > code ;) > > > def GetPrimes(N): > > arr = [] > > for i in range(1,N+1): > > arr.ap

Re: Little novice program written in Python

2008-04-25 Thread Arnaud Delobelle
hellt <[EMAIL PROTECTED]> writes: > my variant of the sieve Since you posted it, you are also looking for advice to improve your code ;) > def GetPrimes(N): > arr = [] > for i in range(1,N+1): > arr.append(i) This is the same as: arr = range(1, N+1) !-) > #Set first i

Re: Little novice program written in Python

2008-04-25 Thread hellt
also, i would recommend you to visit projecteuler.net you can solve math tasks and then see how others have done the same. you can fetch very good and pythonic solution there. -- http://mail.python.org/mailman/listinfo/python-list

Re: Little novice program written in Python

2008-04-25 Thread hellt
Rogério Brito: > Hi, All. > > I'm just getting my feet wet on Python and, just for starters, I'm coding some > elementary number theory algorithms (yes, I know that most of them are already > implemented as modules, but this is an exercise in learning the language > idioms). > > As you can see f

Re: Little novice program written in Python

2008-04-25 Thread Robert Bossy
John Machin wrote: On Apr 25, 5:44 pm, Robert Bossy <[EMAIL PROTECTED]> wrote: Peter Otten wrote: Rogério Brito wrote: i = 2 while i <= n: if a[i] != 0: print a[i] i += 1 You can spell this as a for-loop: for p in a: if p: print p

Re: Little novice program written in Python

2008-04-25 Thread John Machin
On Apr 25, 5:44 pm, Robert Bossy <[EMAIL PROTECTED]> wrote: > Peter Otten wrote: > > Rogério Brito wrote: > > >> i = 2 > >> while i <= n: > >> if a[i] != 0: > >> print a[i] > >> i += 1 > > > You can spell this as a for-loop: > > > for p in a: > > if p: > > print p > >

Re: Little novice program written in Python

2008-04-25 Thread Robert Bossy
Peter Otten wrote: Rogério Brito wrote: i = 2 while i <= n: if a[i] != 0: print a[i] i += 1 You can spell this as a for-loop: for p in a: if p: print p It isn't exactly equivalent, but gives the same output as we know that a[0] and a[1] are also 0. I

Re: Little novice program written in Python

2008-04-25 Thread Peter Otten
Rogério Brito wrote: > i = 2 > while i <= n: >      if a[i] != 0: > print a[i] >      i += 1 You can spell this as a for-loop: for p in a: if p: print p It isn't exactly equivalent, but gives the same output as we know that a[0] and a[1] are also 0. Peter -- http://mail.pyt

Re: Little novice program written in Python

2008-04-24 Thread castironpi
On Apr 24, 11:09 pm, Dennis Lee Bieber <[EMAIL PROTECTED]> wrote: > On Thu, 24 Apr 2008 21:31:15 -0300, Rogério Brito <[EMAIL PROTECTED]> > declaimed the following in comp.lang.python: > > > a = [i for i in range(0,n+1)] > >         Uhm... At least in 2.4 and earlier, range() returns a list... No >

Re: Little novice program written in Python

2008-04-24 Thread Steve Holden
Rogério Brito wrote: Hi, All. I'm just getting my feet wet on Python and, just for starters, I'm coding some elementary number theory algorithms (yes, I know that most of them are already implemented as modules, but this is an exercise in learning the language idioms). As you can see from t

Re: Little novice program written in Python

2008-04-24 Thread Raymond Hettinger
> What I would like is to receive some criticism to my code to make it more > Python'esque and, possibly, use the resources of the computer in a more > efficient way (the algorithm implemented below is the Sieve of Eratosthenes): It looks like straight-forward code and is fine as it stands. If you

Little novice program written in Python

2008-04-24 Thread Rogério Brito
Hi, All. I'm just getting my feet wet on Python and, just for starters, I'm coding some elementary number theory algorithms (yes, I know that most of them are already implemented as modules, but this is an exercise in learning the language idioms). As you can see from the code below, my backg