Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Little novice program written in Python

Reply
Thread Tools

Little novice program written in Python

 
 
Rogério Brito
Guest
Posts: n/a
 
      04-25-2008
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 background is in C, without too much
sophistication.

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):

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#!/usr/bin/env python

n = int(raw_input())
a = [i for i in range(0,n+1)]
a[1] = 0 # not a prime
prime = 1 # last used prime
finished = False

while (not finished):
prime = prime + 1
# find new prime
while prime*prime <= n and a[prime] == 0:
prime += 1
# cross the composite numbers
if prime*prime <= n:
j = 2*prime
while j <= n:
a[j] = 0
j += prime
else:
finished = True

# print out the prime numbers
i = 2
while i <= n:
if a[i] != 0:
print a[i]
i += 1
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Thank you for any help in improving this program,

--
Rogério Brito : rbrito@{mackenzie,ime.usp}.br : GPG key 1024D/7C2CAEB8
http://www.ime.usp.br/~rbrito : http://meusite.mackenzie.com.br/rbrito
Projects: algorithms.berlios.de : lame.sf.net : vrms.alioth.debian.org
 
Reply With Quote
 
 
 
 
Raymond Hettinger
Guest
Posts: n/a
 
      04-25-2008
> 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 want to tweak it a bit, you can avoid using a flag like
"finished" by using a break-statement.


Raymond
 
Reply With Quote
 
 
 
 
Steve Holden
Guest
Posts: n/a
 
      04-25-2008
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 the code below, my background is in C, without too
> much sophistication.
>
> 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):
>
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
> #!/usr/bin/env python
>
> n = int(raw_input())
> a = [i for i in range(0,n+1)]
> a[1] = 0 # not a prime
> prime = 1 # last used prime
> finished = False
>
> while (not finished):
> prime = prime + 1
> # find new prime
> while prime*prime <= n and a[prime] == 0:
> prime += 1
> # cross the composite numbers
> if prime*prime <= n:
> j = 2*prime
> while j <= n:
> a[j] = 0
> j += prime
> else:
> finished = True
>
> # print out the prime numbers
> i = 2
> while i <= n:
> if a[i] != 0:
> print a[i]
> i += 1
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
>
> Thank you for any help in improving this program,
>

Your Python is actually pretty good - if Raymond Hettinger pronounces it
OK then few would dare to disagree.

As for your English, though, the word you sought was "Pythonic" (not
that you will ever find such a word in Webster's dictionary). To suggest
that your code is Pythonesque would mean you found it farcical or
ridiculous (like a Monty Python sketch), which it clearly is not.

Another wrinkle you might consider is simply printing the primes out as
they are generated rather than doing the printing in a separate loop,
though whether that approach would be preferable in "real life" would
depend on the application, of course.

regards
Steve

PS: I think either my mailer or yours has mangled the indentation.
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

 
Reply With Quote
 
castironpi@gmail.com
Guest
Posts: n/a
 
      04-25-2008
On Apr 24, 11:09*pm, Dennis Lee Bieber <(E-Mail Removed)> wrote:
> On Thu, 24 Apr 2008 21:31:15 -0300, Rogério Brito <(E-Mail Removed)>
> 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 that era... range() also begins with 0
>
>
>
> >>> n = 5
> >>> a = range(n+1)
> >>> a

> [0, 1, 2, 3, 4, 5]
>
> * * * * So just
>
> * * * * a = range(n+1)
>
> could be used. Of course, if using a version where range() and xrange()
> have been unified...
>
> >>> c = list(xrange(n+1))
> >>> c

> [0, 1, 2, 3, 4, 5]
>
> --
> * * * * Wulfraed * * * *Dennis Lee Bieber * * * * * * * KD6MOG
> * * * * (E-Mail Removed) * * * * * * *(E-Mail Removed)
> * * * * * * * * HTTP://wlfraed.home.netcom.com/
> * * * * (Bestiaria Support Staff: * * * * * * * (E-Mail Removed))
> * * * * * * * * HTTP://www.bestiaria.com/


You're talking hardware-native, which machines don't guarantee.
Python can in another dimension of machine compatibility. Stacks are
hardware native, the location of an array is not. Python can retrieve
your stack in higher dimensions.

Fortunately, Python's community is sturdy against counterproductivity
en masse, so it's okay to hairbrain it. Cover features of
improvements, though, and you might get a Bayes Net change to make and
courses to steer. The community values the flexibility of machine-
independency too.

However, real numbers are not integers, so opinion mass of integer
algorithms may favor C. But you just need micro-sales (and scales!)
to examine the future of Python. Welcome to our group.
 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      04-25-2008
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
 
Reply With Quote
 
Robert Bossy
Guest
Posts: n/a
 
      04-25-2008
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.
>

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


Cheers,
RB
 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      04-25-2008
On Apr 25, 5:44 pm, Robert Bossy <(E-Mail Removed)> 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

>
> > It isn't exactly equivalent, but gives the same output as we know that a[0]
> > and a[1] are also 0.

>
> 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 of memory required.
 
Reply With Quote
 
Robert Bossy
Guest
Posts: n/a
 
      04-25-2008
John Machin wrote:
> On Apr 25, 5:44 pm, Robert Bossy <(E-Mail Removed)> 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
>>>
>>> It isn't exactly equivalent, but gives the same output as we know that a[0]
>>> and a[1] are also 0.
>>>

>> 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 of memory required.

Indeed. Would it be a sensible proposal that sequence slices should
return an iterator instead of a list?

RB
 
Reply With Quote
 
hellt
Guest
Posts: n/a
 
      04-25-2008


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 background is in C, without too much
> sophistication.
>
> 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):
>


my variant of the sieve

def GetPrimes(N):
arr = []
for i in range(1,N+1):
arr.append(i)
#Set first item to 0, because 1 is not a prime
arr[0]=0
#sieve processing
s=2
while s < math.sqrt(N):
if arr[s-1] != 0:
j = s*s
while j <= N:
arr[j-1] = 0
j += s
s += 1
return [x for x in arr if x != 0]
 
Reply With Quote
 
hellt
Guest
Posts: n/a
 
      04-25-2008
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Generally, are the programs written by C++ slower than written by C10% ? KaiWen C++ 102 09-14-2011 11:12 PM
1 little 2 little 3 little Kennedys dale Digital Photography 0 03-23-2008 01:03 PM
[python] Is there a python written fax program out there? David Stockwell Python 2 06-08-2004 08:28 PM
Little job for novice programmer Roedy Green Java 0 09-09-2003 02:30 AM
Re: Can a usercontrol written in C# be used in Web Forms that is written in VB.Net? Steve C. Orr, MCSD ASP .Net 1 08-24-2003 12:06 AM



Advertisments