Re: [Tutor] making lists of prime numbers

2011-09-15 Thread Alan Gauld

On 15/09/11 03:01, c smith wrote:


am I passing arguments from the command line correctly?


Yes, although not converting to an int.


this seems to count by twos (going through both 'if statements' maybe?)
I thought the 'break' would send control back to the 'while'
basically, is there an obvious problem here or is it just completely wrong?


It might work once you iron out the bugs but it's horribly inefficient. 
It will take a very long time to run for large numbers. Did you try 
searching for prime number algorithms? There are several that will be 
much faster than this.


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] making lists of prime numbers

2011-09-15 Thread Dave Angel

On 09/14/2011 10:35 PM, Andre Engels wrote:

On Thu, Sep 15, 2011 at 4:01 AM, c smithwrote:


hi list, i am trying the MIT opencourseware assignments.
one was to find the 1000th prime.
since this isn't actually my homework, I modified the solution as I would
like to collect lists of primes and non-primes up to N, also some log()
ratio to one comparison.
here is what I came up with on paper:
#!/usr/bin/env python
import sys, os, math

def main(a):
 notprime = []
 primelist = [2,3]
 nprime = sys.argv[1]
 numcheck = 4
 while len(primelist)<  nprime:
 for i in range(2,numcheck-1):
 if numcheck % i == 0:
 print numcheck,'is not prime'
 notprime.append(numcheck)
 numcheck += 1
 break
 if i == numcheck and numcheck % i !=0:
 print numcheck, 'is prime'
 primelist.append(numcheck)
 numcheck += 1
 break
 TwotoN = 0
 for j in primelist:
 TwotoN += log(j)
 print 'sum of logs of primes from 2 to', nprime,'is',TwotoN
 print 'the ratio of this sum to 1 is' %f % (float(TwotoN)/nprime)
if __name__=='__main__':
 main(sys.argv[1])

my questions would be:
am I passing arguments from the command line correctly?
this seems to count by twos (going through both 'if statements' maybe?)
I thought the 'break' would send control back to the 'while'
basically, is there an obvious problem here or is it just completely wrong?


I'd say the obvious problem is your second if statement. Its guard will
never be true. i goes through range(2,numcheck-1). The highest number in
that range in numcheck-2, so i==numcheck will never be true. Even if you'd
get i to equal numcheck somehow, numcheck % i would then ber equal to
numcheck % numcheck, which equals 0, so numcheck % i != 0 would not be true.

The obvious thing to do seems to be to get this code out of the for loop,
without and if statement guarding it.

Another thing that goes wrong is:

nprime = sys.argv[1]

You use nprime as an integer, but sys.argv[1] is a string. You should thus
change this into

nprime = int(sys.argv[1])

However, for the sake of reusability, it's better to have a function that
gets the first n primes for n decided by the caller than to have a function
that gets the first n primes for n always being the first argument, thus I
would change it to:

nprime = int(a)

As  Andre and Alan have said, there are some problems with your code.  
However, the basic concept is reasonable.  I wouldn't worry much about 
efficiency, yet.  Get it to work reliably, then figure how to tune it.  
And if you have a source control system (like git, svn, mercurial), 
check in each version that works, so you can always compare to what you 
currently have.


Your specific questions:
  1) you're passing the (one) argument from the command line 
reasonably, though I'd convert it to int before passing it, and I'd 
change the formal parameter name of it in your function to nprime.  Then 
you don't need any additional assignment in the function.
  2) it seems to count by twos because of the problems with your second 
if, as Andre has said.


Start by fixing the second if statement.  There are two ways to fix it.
a) change the condition so it detects the end of the loop, which in 
your case is numcheck-2.  Catch to that is that when you realize you can 
optimize the loop, you'll have to remember to change the if to match.
b) move the code outside of the loop.  However, you'd also have to 
make sure it *didn't* get executed if the first if statement did.  That 
could be a real pain, except that Python has a nice "else" clause you 
can attach to a loop, that only gets executed if the loop completes 
normally.  That else is exactly what you want, so it makes it 
straightforward.


Once you see it generating the correct primes, you may notice that it 
sails right past the nprimes value.  That'll get fixed when you add the 
int() function when calling main().  Comparing int to string usually 
doesn't do what you'd want.


next problem you'll have is the log function, which is defined in math.  
So you want to call math.log()


next problem is the misplaced quotes on the last print statement.  
You'll get an error when you hit that one.


Finally, the wording of those two prints is all wrong, so you might want 
to fix it so it matches what the code does.


HTH


--

DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] What's the keyword for the Python creed?

2011-09-15 Thread Richard D. Moores
You know, at the interactive prompt you enter some Monty Python word
that I can't remember, and you get a small list of pithy pythonic
advice such as "explicit is better than implicit", etc.

Thanks,

Dick Moores
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What's the keyword for the Python creed?

2011-09-15 Thread Scott Nelson
On Thu, Sep 15, 2011 at 1:19 PM, Richard D. Moores wrote:

> You know, at the interactive prompt you enter some Monty Python word
> that I can't remember, and you get a small list of pithy pythonic
> advice such as "explicit is better than implicit", etc.
>

import this

You can also do...

import antigravity

...for another Pythonic easter egg...  Though I think this one requires
Python 3?  2.7?  Can't remember.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What's the keyword for the Python creed?

2011-09-15 Thread Jordan
I think you mean the Zen of Python:
http://www.python.org/dev/peps/pep-0020/
The command is
>>> import this
--
Jordan
On 09/15/2011 11:19 AM, Richard D. Moores wrote:
> You know, at the interactive prompt you enter some Monty Python word
> that I can't remember, and you get a small list of pithy pythonic
> advice such as "explicit is better than implicit", etc.
>
> Thanks,
>
> Dick Moores
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What's the keyword for the Python creed?

2011-09-15 Thread Prasad, Ramit
>import antigravity
>...for another Pythonic easter egg...  Though I think this one requires Python 
>3?  2.7?  Can't remember.

Works in 2.7 but not 2.6.4


Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423



This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What's the keyword for the Python creed?

2011-09-15 Thread Richard D. Moores
Thanks, all. Good to have that at hand.

antigravity: any more?

Dick
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Please review my code

2011-09-15 Thread Emeka
Hello All,


While trying to learn Python I developed a small game, Text Text.  It has
rough edges and may not meet your class. However, by reviewing it you'll be
helping  me to advance.

It was only tested on a Windows Box, but I see no reason why it would not
work on Unix family. https://github.com/janus/Text-Twist
I need you comments.
-- 
*Regards,
Emeka


*
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] help importing pygame

2011-09-15 Thread Chad Reynolds
For starters, I am a newish python programmer, far enough along to have 
my feet wet.  Running Mac OSX Lion (10.7), and python 2.7.2.  I am trying to 
expand my knowledge and use some packages outside of the base python library.  
Games are interesting so I decided I'd try pygame after reading many 
good things about it.  Now I have been to the site and downloaded the install 
package for OSX Lion, and run it successfully.  But when I try to run the 
example on the site (line by line chimp example) I get this error message:

Traceback (most recent call last):
  File "/private/var/folders/j4/10dtxgh14hl6vdvzzhbbqzdcgn/T/Cleanup At 
Startup/untitled text 4-337832000.071", line 11, in 
import os, pygame
  File 
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pygame/__init__.py",
 line 95, in 
from pygame.base import *
ImportError: 
dlopen(/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pygame/base.so,
 2): no suitable image found.  Did find:

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pygame/base.so:
 no matching architecture in universal wrapper


The program hasn't even made it anywhere else in the code, and I really don't 
understand what the problem is.  Help would be much appreciated, thank you all
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Strange zip syntax

2011-09-15 Thread Brett Ritter
I ran into this article (
http://blog.adku.com/2011/09/hodgepodge-of-python.html ) and found
myself temporarily stymied by one line it in:

zip(*[iter(a)]*2)

Used like this:

>>> a = ['a','1','b','2','c','3']
>>> zip(*[iter(a)]*2)
[('a', '1'), ('b', '2'), ('c', '3')]

While I'm unlikely to use such a construct (if I can't easily follow
it now, I or my successor likely won't follow it should it need to be
debugged sometime in the future), I found the education I got in
deciphering it was worth the effort.  I'm sharing it here so others
can benefit from my puzzlement.

iter(a) returns a list iterator for a.  See help(iter) for more.
[iter(a)] is a list containing one element, an iterator.  This is
created only so we can do the below:
[iter(a)]*2 is a list containing two elements, each the SAME list iterator.
For simplicity, let's break this out for further analysis:

>>> b = iter(a)
>>> c = [b,b]

*[iter(a)]*2 flattens the list when passed into a function call.
Using our more verbose but simple syntax: *c.  This only works when
passed to a function.
zip() creates tuples each holding the Nth elements from a number of
sequences.  See help(zip) for more.
Thus, zip(a) or zip(a,a) would return:
>>> zip(a)
[('a',), ('1',), ('b',), ('2',), ('c',), ('3',)]
>>> zip(a,a)
[('a', 'a'), ('1', '1'), ('b', 'b'), ('2', '2'), ('c', 'c'), ('3', '3')]

What happens when we pass an iterator to zip?  That's not mentioned in
the docstring blurb.
>>> zip(iter(a))
[('a',), ('1',), ('b',), ('2',), ('c',), ('3',)]
Answer: It works as intended.

Now we come to the magic of this little snippet.
zip(iter(a),iter(a)) wouldn't work, because each call to iter(a)
returns a DIFFERENT iterator.
>>> zip(iter(a), iter(a))
[('a', 'a'), ('1', '1'), ('b', 'b'), ('2', '2'), ('c', 'c'), ('3', '3')]

But by creating the list of two elements each of which is the SAME
iterator, as each is asked to iterate it advances the common element
indicator:
>>> zip(*c)
[('a', '1'), ('b', '2'), ('c', '3')]
Notice that the flattening is required, because zip needs to get
multiple arguments:
>>> b = iter(a)  #our original iterator is spent, so we're assigning a new one
>>> c = [b,b]
>>> zip(c)   #Not flattened, is just a single list, like a.
[(,), (,)]
>>> zip(b,b)   # here it is two iterators sent to zip() (though they happen to 
>>> be the SAME iterator)
[('a', '1'), ('b', '2'), ('c', '3')]

I hope some of you enjoy playing with this, and hopefully someone
learned something useful!  While I'm not likely to use the listed
form, I can very well see myself saying:

>>> a = ['a','1','b','2','c','3']   #well, I can see myself using this with 
>>> meaningful variable names
>>> b = iter(a)
>>> zip(b,b)  # Group in sets of 2 elements

-- 
Brett Ritter / SwiftOne
swift...@swiftone.org
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What's the keyword for the Python creed?

2011-09-15 Thread Christian Witts

On 2011/09/15 08:57 PM, Richard D. Moores wrote:

Thanks, all. Good to have that at hand.

antigravity: any more?

Dick
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor




from __future__ import braces
:)
--

Christian Witts
Python Developer

//
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor