Re: [Tutor] Optimisation of prime number program (cont. from finding prime numbers)

2007-09-26 Thread Kalle Svensson
Hi!

Just a followup on this:

On 9/21/07, Kalle Svensson <[EMAIL PROTECTED]> wrote:
> Hi!
>
> On 9/21/07, Boykie Mackay <[EMAIL PROTECTED]> wrote:
> > Ok,I have learnt how to generate prime numbers and how to 'split'
> > input.The above was to help me solve the second 'SPOJ'
> > challenge,PRIME1.The challenge can be found at
> > 
> ...
> > Could you please look at the program (attached) and advise possible
> > optimisations or point me to possible resources that would help solve
> > this challenge.
>
> Interesting problem! I've been experimenting a bit with it, but I'm
> not anywhere near the runtimes shown in the judge system...

After quite a bit of experimentation, I finally managed to write a
program that was accepted by the judge system. It's a C++
implementation of the deterministic Miller-Rabin algorithm. My Python
implementation of the same algorithm is still too slow, though. Has
anyone managed to write a fast enough Python program?

The problem: https://www.spoj.pl/problems/PRIME1/
Miller-Rabin: http://en.wikipedia.org/wiki/Miller-Rabin_primality_test

Regards,
  Kalle
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] New to Python and Linux

2007-09-26 Thread Andreas Kostyrka
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

Well, chmod 0700 is usually not want you want:

a) 0700 gives only read/write/execute rights to the user. Technically
for historical reason permissions can be written as an octal number,
with the first digit being optional, encoding stuff like setuid/setgid
permissions. The last 3 digits code permissions for user, group and
other, one octal digit each. 4 is read, 2 is write and 1 is execute. We
aware that on directories execute permission allows you to access/search
the directory, while read allows you to "read" (==list) the directory.
So a typical permission for scripts would be 755 (read/exec for all,
write on top of that for the owner), or 775 (on distributions that give
each user their own primary group).

b) Specifying permissions as octal numbers is not really necessary
nowadays (and has not been necessary for over a decade, at least on
Linux): chmod a+x => add eXecute rights to All. chmod u=rwx,og=rx => set 755

Andreas

Jason M Barnes wrote:
> Hi,
> 
> Another way to execute your program is to open up your file with
> gedit, and on the very first line type (sans quotes) "#!/usr/bin/env
> python" by itself and save it.  Then, go to the terminal like Michael
> said, and cd into the directory with your file.  Type "chmod 0700
> your-program.py" at the prompt.  This allows you to execute your
> program without invoking python first.  That way when you are cd'ed
> into the directory that contains your program, you can type
> "./your-program.py" at the prompt, and it should run.
> 
> If you're going to switch to linux, then you'll need to learn how to
> make the most of your CLI.  I'd suggest googling for tutorials on the
> linux command line.
> 
> Jason
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFG+gnyHJdudm4KnO0RAgyXAJ9YGKU2OU9p2pnG4YyS15ZLwzj8vACfRAQD
H7jawyovWfkd9fEm36MfBBs=
=FquY
-END PGP SIGNATURE-
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] home_finance.py

2007-09-26 Thread Eric Walker
I think you need to check to see if the remaining amount is less than the 
payment amount.

Eric...

Christopher Spears <[EMAIL PROTECTED]> wrote: I'm working on a problem in 
Chapter 5 of Core Python
Programming(2nd Edition).  I am supposed to write a
script that take an opening balance and a monthly
payment from the user.  The script then displays the
balance and payments like so:


Enter opening balance: 100
Enter monthly payment: 16.13
Amount  Remaining
Pymnt#  PaidBalance
0   0.00100.00
1   16.13   83.87
2   16.13   67.74
3   16.13   51.61
4   16.13   35.48
5   16.13   19.35
6   16.13   3.22
73.22   0.00

Here is what I have written so far:

#!/usr/bin/env python

balance = float(raw_input("Enter opening balance: "))
payment = float(raw_input("Enter monthly payment: "))

print "\tAmount\tRemaining"
print "Pymnt#\tPaid\tBalance"

payment_num = 0
print "%d\t%.2f\t%.2f" % (payment_num,0,balance)

while balance >= 0:
payment_num = payment_num + 1
if balance > 0:
balance = balance - payment
print "%d\t%.2f\t%.2f" %
(payment_num,payment,balance)
else:
payment = balance
 balance = balance - payment
print "%d\t%.2f\t%.2f" %
(payment_num,payment,balance)
 
This is what my script displays:

Enter opening balance: 100
Enter monthly payment: 16.13
Amount  Remaining
Pymnt#  PaidBalance
0   0.00100.00
1   16.13   83.87
2   16.13   67.74
3   16.13   51.61
4   16.13   35.48
5   16.13   19.35
6   16.13   3.22
7   16.13   -12.91

I'm not sure how to solve this problem.  Apparently,
trying to catch the remaining balance when it becomes
negative doesn't work!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


   
-
Luggage? GPS? Comic books? 
Check out fitting  gifts for grads at Yahoo! Search.___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] copy files + directory tree via ftp

2007-09-26 Thread Nelson Kusuma
Hello

I have a problem when copy files and directory tree in
ftp, scripts in here only copy one file:
from ftplib import FTP
rootList = []
session = FTP()
session.connect('workstation', port=21)
session.login(user='saiki', passwd='saiki')
session.retrlines('LIST', rootList.append)
f=open('D:/PARAMS.LST','rb')
session.storbinary('STOR '+"af", f, 1024)
f.close()
session.close()

f anyone has any ideas I would appreciate it. Thank
you

Nelson


   

Take the Internet to Go: Yahoo!Go puts the Internet in your pocket: mail, news, 
photos & more. 
http://mobile.yahoo.com/go?refer=1GNXIC
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Optimisation of prime number program (cont. from finding prime numbers)

2007-09-26 Thread Kent Johnson
Kalle Svensson wrote:
> After quite a bit of experimentation, I finally managed to write a
> program that was accepted by the judge system. It's a C++
> implementation of the deterministic Miller-Rabin algorithm. My Python
> implementation of the same algorithm is still too slow, though. Has
> anyone managed to write a fast enough Python program?

Have you tried using psyco?
Have you seen this thread? It has some very interesting ideas about 
speeding up the sieve algorithm. (Uncle Timmy is Tim Peters who still 
has one of the fastest Python solutions.)
https://www.spoj.pl/forum/viewtopic.php?t=3226

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] copy files + directory tree via ftp

2007-09-26 Thread Kent Johnson
Nelson Kusuma wrote:
> Hello
> 
> I have a problem when copy files and directory tree in
> ftp, scripts in here only copy one file:

To transfer an entire directory with ftplib you will have to read the 
local directory with os.listdir() and loop to send the files.

There are a few higher-level modules written on top of ftplib that might 
be helpful directly or as examples:
http://pypi.python.org/pypi/ftputil/2.2.3 (site is down at the moment)
http://www.nedbatchelder.com/code/modules/ftpupload.html

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] home_finance.py

2007-09-26 Thread Ricardo Aráoz
Eric Walker wrote:
> I think you need to check to see if the remaining amount is less than
> the payment amount.
> 
> Eric...

Or just subtract the minimum between the remaining amount and the
payment amount. See below

> 
> */Christopher Spears <[EMAIL PROTECTED]>/* wrote:
> 
> I'm working on a problem in Chapter 5 of Core Python
> Programming(2nd Edition). I am supposed to write a
> script that take an opening balance and a monthly
> payment from the user. The script then displays the
> balance and payments like so:
> 
> 
> Enter opening balance: 100
> Enter monthly payment: 16.13
> Amount Remaining
> Pymnt# Paid Balance
> 0 0.00 100.00
> 1 16.13 83.87
> 2 16.13 67.74
> 3 16.13 51.61
> 4 16.13 35.48
> 5 16.13 19.35
> 6 16.13 3.22
> 7 3.22 0.00
> 
> Here is what I have written so far:
> 
> #!/usr/bin/env python
> 
> balance = float(raw_input("Enter opening balance: "))
> payment = float(raw_input("Enter monthly payment: "))
> 
> print "\tAmount\tRemaining"
> print "Pymnt#\tPaid\tBalance"
> 
> payment_num = 0
> print "%d\t%.2f\t%.2f" % (payment_num,0,balance)
> 
> while balance >= 0:
> payment_num = payment_num + 1
> if balance > 0:

> balance = balance - payment
Instead of this ^^^ line you should have :
  balance = balance - min(balance, payment)
And I guess that would be it.

> print "%d\t%.2f\t%.2f" %
> (payment_num,payment,balance)
> else:
> payment = balance
> balance = balance - payment
> print "%d\t%.2f\t%.2f" %
> (payment_num,payment,balance)
> 
> This is what my script displays:
> 
> Enter opening balance: 100
> Enter monthly payment: 16.13
> Amount Remaining
> Pymnt# Paid Balance
> 0 0.00 100.00
> 1 16.13 83.87
> 2 16.13 67.74
> 3 16.13 51.61
> 4 16.13 35.48
> 5 16.13 19.35
> 6 16.13 3.22
> 7 16.13 -12.91
> 
> I'm not sure how to solve this problem. Apparently,
> trying to catch the remaining balance when it becomes
> negative doesn't work!
> ___
> Tutor maillist - Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> 
> 
> Luggage? GPS? Comic books?
> Check out fitting gifts for grads
> 
> at Yahoo! Search.
> 
> 
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python problem

2007-09-26 Thread bhaaluu
Greetings Chris,

On 9/25/07, Chris <[EMAIL PROTECTED]> wrote:
>
>   Ok as I see it, it's should go like this:
>
> Print 'Think of a number between 1 and 100, and let me guess it'
>
> Type1 = input ('Type 1 if I am high')
> Type2 = input ('Type 2 if I am low')
> Type3 = input ('Type 3 if I am dead on')
>
> I can't seem to get the guts of it.  I assume that there are 3 if statements
> and a while statement.
>
> guess
>
> While x !=
> If guess
>
>
>
> I get lost right around here...help!

Thunk! er, Think! =)
Play the guess-a-number game, and jot down exactly what YOU do
to make a guess.

Prompt:> Think of a number between 1 and 100. Got it?
Prompt:> Okay, now I, the Great Computer, will guess the number...
Prompt:> Is the number 51?
Prompt:> 0=Correct, 1=Lower, 2=Higher: _

The range is 1 to 100. Let's say you think of the number 37.
At he prompt, you type 1 because the number is Lower than 51.
(or whatever scheme you decide to use.)

If you were playing against the computer, and the computer told you
to guess Lower, what would you do? Would you guess 50? 49? 10? 1?
How do you guess when you're playing the game?
How do you narrow it down? Figure it out.

So, now you think you can outsmart the computer half-way through
the game by thinking of a different number, say 38 Pretty sneaky!
That's like palming the pea in a shell game. Granted, the Great
Computer, isn't very smart... but it IS an idiot savant. It can remember
tons of information that you've forgotten as irrelevant just seconds ago.
So what the Great Computer has been doing, while guessing, is keeping
track of the bounds it can guess between, from the hints you've been
providing [0,1,2, whatever] and maybe even a list of all its guesses that
were not correct. And in a split second, it can compare all that stuff.

Chris, what constraints are you working under? How many Python 'tools'
(keywords, methods, etc.) do you have to solve the problem with?
Input? [raw_inpu(), input(), ...], Output? [print, ...], Selection?
[if, elif, else, ...]
Iteration? [for, while, ...]. You also have to consider what you have
to work with
so far.

-- 
b h a a l u u at g m a i l dot c o m
http://www.geocities.com/ek.bhaaluu/index.html

> import random
> > number = random.randint(1, 101)
> > print "I've thought of a number between 1 and 100."
> > print "Try and guess it!"
> > print
> > guess = input( "What's your guess? ")
> > while guess != number:
> > if guess > number:
> > print "Your guess is too high."
> > else: #elif guess < number:
> > print "Your guess is too low."
> > guess = input( "What's your next guess? " ) print
> > "Congratulations! You guessed the number."
> -Original Message-
> From: Kent Johnson [mailto:[EMAIL PROTECTED]
> Sent: Tuesday, September 25, 2007 7:32 PM
> To: Chris
> Cc: tutor@python.org
> Subject: Re: [Tutor] python problem
>
> Chris wrote:
>
> > Guess the number from the user using 1,2,3 keys
> >   Ask the user if # is high, low, or dead on.
> >   Print 'you got it' when Python gets it.
> >
> > As you can see I need help. I've given you the pseudo code.
>
> OK, how about some real code? If we write the program for you that won't
> help you learn. What have you tried so far? Which parts of the
> pseudocode do you know how to code? Which parts do you need help with?
>
> Kent
>
>
>
>
> No virus found in this incoming message.
> Checked by AVG Free Edition.
> Version: 7.5.487 / Virus Database: 269.13.25/1018 - Release Date: 9/19/2007
> 3:59 PM
>
>
> No virus found in this outgoing message.
> Checked by AVG Free Edition.
> Version: 7.5.487 / Virus Database: 269.13.25/1018 - Release Date: 9/19/2007
> 3:59 PM
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python problem

2007-09-26 Thread Kent Johnson
Chris wrote:
>   Ok as I see it, it's should go like this:
> 
> Print 'Think of a number between 1 and 100, and let me guess it'
> 
> Type1 = input ('Type 1 if I am high')
> Type2 = input ('Type 2 if I am low')
> Type3 = input ('Type 3 if I am dead on')

This will ask for three different inputs which is not what you want. Can 
you modify it to ask for input just once?
> 
> I can't seem to get the guts of it.  I assume that there are 3 if statements
> and a while statement.

That sounds about right.
> 
> guess
> 
> While x != 
>   If guess 

OK, you have some of the pieces. Maybe it would help to write a more 
detailed pseudocode. I think it might be:

ask the user to pick a number
wait for the user to pick a number
pick a starting guess
loop:
   tell the user my guess
   ask if the guess is correct
   if the guess is correct
 congratulate myself
 quit
   if the guess is too high
 pick a lower guess
   if the guess is too low
 pick a higher guess

Can you turn this into code?

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] New to Python and Linux

2007-09-26 Thread bhaaluu
Greetings,

I don't know how to do ms-windows, but I use GNU/Linux on a daily
basis. There is a nice IDE (Integrated Development Environment) for
Python called IDLE. It is available for ms-windows, GNU/Linux, and maybe
another OS as well. There is information about IDLE at Python.Org.  I'd
suggest that you Check It Out. If it isn't already installed on your FedoraCore
distro, you'll have to download it.  http://www.python.org/idle/
IDLE has a shell, an editor, a debugger, and all sorts of bells and whistles.

What you'll find out about GNU/Linux is: it isn't very difficult at all.
you're just unfamiliar with it. The unfamiliarity makes it seem difficult. =)
It's actually easier to use than ms-windows, once you become familiar
with it, much easier.  You should see me trying to do anything on a
ms-windows computer... it seems so braindead to me, I flop around
like a fish out of water because I'm not familiar with it. Okay, enuff of that.
(That was my version of encouragement. =)

I use vim.
vim is "vi improved".
vi (pronounced "vee-eye") is the 'vi'sual editor.
There is some version of vi on just about every *nix box out there.
vi has been around since almost the very beginning of creation.
(the beginning of creation is counted from 1970-01-01 or thereabouts...
The vi editor was developed starting around 1976 by Bill Joy, who was
then a graduate student at the University of California at Berkeley.
... )

So, why vim, and not an easier editor, like pico or nano, or a swiss-army-editor
like emacs, or a GUI editor like Gedit, or Nedit, or Kate, or Kwrite?
pico and nano are extremely limited. Yeah, they're easy: too easy!
You can't really do anything serious with them.
Your favorite GUI editor might not be available. I once watched a couple
of guys who were thrown out of X to a console, trying to edit a config file
to get X going, using vi. They didn't have their favorite GUI editor, and
they were completely lost. It was hilarious to watch them. I let them
suffer for a few minutes before offering suggestions. Too funny! 8^D
emacs? I won't go there... it's like dicussing religion, or something. There
are Python bindings for emacs, which will allow you to run it like an IDE.

vim is ubiquitous, powerful, and very, very sexy.  That last part was to
make you curious enough to try it. Do you have vim on your fedoracore?
Maybe. I always have to download it with a new install of my distro.
nvi is the default version of vi with a new install of my distro.

vim.
Open a terminal and type: which vim
If you get something like: /usr/bin/vim
then you're in business. Otherwise, you'll have to grab it and install it.
Once you have it, you start it by typing vim at the prompt.
Once in vim, press Shift-colon ( : ) help
:help
To exit vim, press Shift-colon q
:q
(you may have to do it a couple of times, if you're in help)

vim comes with an interactive tutorial called: vimtutor
Just type: vimtutor at the prompt to start it. It has enough stuff in it
to get you started. Once you've started, you can pick up more
advanced stuff as you need it.

Now the part you've been waiting for!
To make vim a Python IDE, copy/paste this file into your home directory:

" .vimrc
"
" Created by Jeff Elkner 23 January 2006
" Last modified 2 February 2006
"
" Turn on syntax highlighting and autoindenting
syntax enable
filetype indent on
" set autoindent width to 4 spaces (see
" http://www.vim.org/tips/tip.php?tip_id=83)
set nu
set et
set sw=4
set smarttab
" Bind  key to running the python interpreter on the currently active
" file.  (curtesy of Steve Howell from email dated 1 Feb 2006).
map  :w\|!python %

Now edit your Python code in an editor that has line numbers
(my addition to the original file, found at:
http://www.ibiblio.org/obp/pyBiblio/tips/elkner/vim4python.php )
syntax highlighting, auto-indent, AND, just press the F2 function
key to run the code from the editor (no quitting the editor, running
the code, starting the editor).

Here is an interesting and helpful Visual vi tutorial:
http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html

Finally, from the same site:
"Why, oh WHY, do those [EMAIL PROTECTED] nutheads use vi?"
http://www.viemu.com/a-why-vi-vim.html

What fun, eh? ;-)
-- 
b h a a l u u at g m a i l dot c o m
http://www.geocities.com/ek.bhaaluu/index.html

On 9/26/07, Armand Nell <[EMAIL PROTECTED]> wrote:
> Hi
>
> I am new to python programming and also the linux enviroment most of my
> skills are windows based and programming skills is visual basics. I decided
> that it would be a great start and new direction for me to learn python and
> at the same time linux. However I have already run into a wall, and any help
> would be appreciated even if you can direct me where to find the info or
> 'turor'.
>
> I am running Fedoracore 7 and Python 2.5
>
> In windows, if i write a program in Python and save it I then can simply
> double click the icon and the program will execute in a console window. Now
> under Fedoraco

[Tutor] indexing question

2007-09-26 Thread Emad Nawfal
Hi Tutors,
Is there a way to get the index of every occurence of a certain element in a
list. listname.index(element) gives me the index of the first occurence. For
example:

>>> t = 'we are we are we we we'.split()
>>> t
['we', 'are', 'we', 'are', 'we', 'we', 'we']

>>> for word in t:
 if word =='we':
  t.index(word)


0
0
0
0
0

Now I want the index of each 'we' in t. I want the result to be 0, 2, 4, 5,
6

How can I do that?

Thank you in anticipation?

-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد
الغزالي
Emad Soliman Nawfal
Indiana University, Bloomington
http://emadnawfal.googlepages.com

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] indexing question

2007-09-26 Thread Kent Johnson
Emad Nawfal wrote:
> Hi Tutors,
> Is there a way to get the index of every occurence of a certain element 
> in a list. listname.index(element) gives me the index of the first 
> occurence. For example:
> 
>  >>> t = 'we are we are we we we'.split()

> Now I want the index of each 'we' in t. I want the result to be 0, 2, 4, 
> 5, 6

In [1]: t = 'we are we are we we we'.split()
In [2]: [i for i, x in enumerate(t) if x=='we']
Out[2]: [0, 2, 4, 5, 6]

Or, IMO a bit awkward but you could do this:

In [3]: [i for i in range(len(t)) if t[i]=='we']
Out[3]: [0, 2, 4, 5, 6]

In [4]: filter(lambda i: t[i]=='we', range(len(t)))
Out[4]: [0, 2, 4, 5, 6]

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Re: New to Python and Linux

2007-09-26 Thread 林培恒
Hi Armand, I suggest you learn Linux command line first. It is the 
basic skill in Linux world, like double-click in Windows. If you know how to 
run a program in Windows with double-click, you will understand how to run most 
program in Windows. If you know how to run a program with command line, you 
will understand how to run most program in Linux. Linux command line may be 
some more difficult than double-click, but it is worthy. You can learn Linux 
command line from here:

 http://linuxcommand.org

 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Re: New to Python and Linux

2007-09-26 Thread 林培恒
Hi Armand, I suggest you learn Linux command line first. It is the 
basic skill in Linux world, like double-click in Windows. If you know how to 
run a program in Windows with double-click, you will understand how to run most 
program in Windows. If you know how to run a program with command line, you 
will understand how to run most program in Linux. Linux command line may be 
some more difficult than double-click, but it is worthy. You can learn Linux 
command line from here:

 http://linuxcommand.org

 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Python for animation

2007-09-26 Thread Babajide, Owojaiye
Hi all,
I'm looking for a tutorial PDF file of Python that is dedicated to
animation and special effects,can ahyone help?If you can please forward
to the following address:

[EMAIL PROTECTED]

[EMAIL PROTECTED]


THANKS


This email and any files transmitted with it are confidential and intended 
solely for the use of the individual or entity to which they are addressed. 
If you have received this email in error please notify the Diageo Servicedesk 
on +44 (0) 131 319 6000

This footnote also confirms that this email has been scanned for all viruses by 
the Messagelabs SkyScan service.

http://www.diageo.com

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] questions about tuples

2007-09-26 Thread Fangwen Lu
Dear all-

 I want to do some loops. Each loop will generate a tuple. Eventually I want
to put tuples together in a higher level of tuple. Do you know how to do
this?

Say a=(1,2,3), b=(4,3,2),c=(9,5,6). I want to get a tuple as ((1,2,3),
(4,3,2),(9,5,6)).

If there are just 2 tuples, I can write x=a and then x=(x,b). But for 3
tuples or more, I will get something like (((1,2,3), (4,3,2)),(9,5,6)).

Thanks.

Fangwen
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] print array

2007-09-26 Thread Fangwen Lu
Dear all-

Let a=(3,4,7), b=[3,4,7].
If I type
print '%d + %d = %d' %(a)
I get
3 + 4 = 7

But if I type  print '%d + %d = %d' %(b), I get errors.

If there is a way for doing this kind of type dirrectly from array.

Thank you!

Fangwen
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] how to convert array into tuple

2007-09-26 Thread Fangwen Lu
Dear all-

If I have an array array([1, 2, 3, 4, 5, 6]), and I want to get
((1,2),(3,4),(5,6)). What should I do?

Thank you!

Fangwen
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] questions about tuples

2007-09-26 Thread Ricardo Aráoz
Fangwen Lu wrote:
> Dear all-
>  
> I want to do some loops. Each loop will generate a tuple. Eventually I
> want to put tuples together in a higher level of tuple. Do you know how
> to do this?
>  
> Say a=(1,2,3), b=(4,3,2),c=(9,5,6). I want to get a tuple as ((1,2,3),
> (4,3,2),(9,5,6)).
>  
> If there are just 2 tuples, I can write x=a and then x=(x,b). But for 3
> tuples or more, I will get something like (((1,2,3), (4,3,2)),(9,5,6)).
>  
> Thanks.
>  
> Fangwen
> 

>>> a = (1,2,3)
>>> b = (4,5,6)
>>> c = (7,8,9)
>>> x = (a,b,c)
>>> x
((1, 2, 3), (4, 5, 6), (7, 8, 9))

Or :

>>> x = []
>>> x.append(a)
>>> x.append(b)
>>> x.append(c)
>>> tuple(x)
((1, 2, 3), (4, 5, 6), (7, 8, 9))

HTH


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] print array

2007-09-26 Thread Eric Brunson
Fangwen Lu wrote:
> Dear all-
>  
> Let a=(3,4,7), b=[3,4,7].
> If I type
> print '%d + %d = %d' %(a)
> I get
> 3 + 4 = 7
>  
> But if I type  print '%d + %d = %d' %(b), I get errors.
>  
> If there is a way for doing this kind of type dirrectly from array.

No, but it's trivial to convert an array to a tuple:

 >>> b=[3,4,7]
 >>> print '%d + %d = %d' % tuple(b)
3 + 4 = 7

>  
> Thank you!
>  
> Fangwen
> 
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>   

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] questions about tuples

2007-09-26 Thread Eric Brunson
Fangwen Lu wrote:
> Dear all-
>  
> I want to do some loops. Each loop will generate a tuple. Eventually I 
> want to put tuples together in a higher level of tuple. Do you know 
> how to do this?
>  
> Say a=(1,2,3), b=(4,3,2),c=(9,5,6). I want to get a tuple as ((1,2,3), 
> (4,3,2),(9,5,6)).
>  
> If there are just 2 tuples, I can write x=a and then x=(x,b). But for 
> 3 tuples or more, I will get something like (((1,2,3), (4,3,2)),(9,5,6)).
 >>> a=(1,2,3)
 >>> b=(4,3,2)
 >>> c=(9,5,6)
 >>> x = (a,b,c)
 >>> print x
((1, 2, 3), (4, 3, 2), (9, 5, 6))


Or:

 >>> x = []
 >>> x.append(a)
 >>> x.append(b)
 >>> x.append(c)
 >>> print x
[(1, 2, 3), (4, 3, 2), (9, 5, 6)]
 >>> x = tuple(x)
 >>> print x
((1, 2, 3), (4, 3, 2), (9, 5, 6))


You can't use append() on a tuple, because a tuple is, by design, immutable.
>  
> Thanks.
>  
> Fangwen
> 
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>   

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to convert array into tuple

2007-09-26 Thread Ricardo Aráoz
Fangwen Lu wrote:
> Dear all-
>  
> If I have an array array([1, 2, 3, 4, 5, 6]), and I want to get
> ((1,2),(3,4),(5,6)). What should I do?
>  

tuple([j for (i,j) in enumerate(zip(x, x[1:])) if i % 2 == 0])

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] print array

2007-09-26 Thread Ricardo Aráoz
Fangwen Lu wrote:
> Dear all-
>  
> Let a=(3,4,7), b=[3,4,7].
> If I type
> print '%d + %d = %d' %(a)
> I get
> 3 + 4 = 7
>  
> But if I type  print '%d + %d = %d' %(b), I get errors.
>  
> If there is a way for doing this kind of type dirrectly from array.
>  

print '%d + %d = %d' % tuple(b)
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to convert array into tuple

2007-09-26 Thread Eric Brunson
Fangwen Lu wrote:
> Dear all-
>  
> If I have an array array([1, 2, 3, 4, 5, 6]), and I want to get 
> ((1,2),(3,4),(5,6)). What should I do?

First, you read some documentation and tutorials on the language, then 
you iterate over the list and build tuples out of the elements.

>  
> Thank you!
>  
> Fangwen
> 
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>   

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python problem

2007-09-26 Thread Chris
Here some more work:


guess = 0

Print ‘Pick a number between 0 and 100’


While guess !=  :
print ‘My first guess is ‘, guess
print ‘Is my guess correct?’
if guess = = type3
print ‘I got it!’   
if guess > ___:
pick lower #
if guess <___ :
pick higher #

that’s all I can do right now…

the mechanism that I can’t figure out is how to show in code form how the
computer goes to the next guess using the split range... ie. if the first
guess is 50, the second guess would be taken from the 50-100 (50/2 = 25 + 50
= 75) second guess  would be 75 if the number is too high.  If the number
falls below 50, then the split range is 25, ect…

-Original Message-
From: bhaaluu [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, September 26, 2007 5:13 AM
To: Chris
Cc: tutor@python.org
Subject: Re: [Tutor] python problem

Greetings Chris,

On 9/25/07, Chris <[EMAIL PROTECTED]> wrote:
>
>   Ok as I see it, it's should go like this:
>
> Print 'Think of a number between 1 and 100, and let me guess it'
>
> Type1 = input ('Type 1 if I am high')
> Type2 = input ('Type 2 if I am low')
> Type3 = input ('Type 3 if I am dead on')
>
> I can't seem to get the guts of it.  I assume that there are 3 if
statements
> and a while statement.
>
> guess
>
> While x !=
> If guess
>
>
>
> I get lost right around here...help!

Thunk! er, Think! =)
Play the guess-a-number game, and jot down exactly what YOU do
to make a guess.

Prompt:> Think of a number between 1 and 100. Got it?
Prompt:> Okay, now I, the Great Computer, will guess the number...
Prompt:> Is the number 51?
Prompt:> 0=Correct, 1=Lower, 2=Higher: _

The range is 1 to 100. Let's say you think of the number 37.
At he prompt, you type 1 because the number is Lower than 51.
(or whatever scheme you decide to use.)

If you were playing against the computer, and the computer told you
to guess Lower, what would you do? Would you guess 50? 49? 10? 1?
How do you guess when you're playing the game?
How do you narrow it down? Figure it out.

So, now you think you can outsmart the computer half-way through
the game by thinking of a different number, say 38 Pretty sneaky!
That's like palming the pea in a shell game. Granted, the Great
Computer, isn't very smart... but it IS an idiot savant. It can remember
tons of information that you've forgotten as irrelevant just seconds ago.
So what the Great Computer has been doing, while guessing, is keeping
track of the bounds it can guess between, from the hints you've been
providing [0,1,2, whatever] and maybe even a list of all its guesses that
were not correct. And in a split second, it can compare all that stuff.

Chris, what constraints are you working under? How many Python 'tools'
(keywords, methods, etc.) do you have to solve the problem with?
Input? [raw_inpu(), input(), ...], Output? [print, ...], Selection?
[if, elif, else, ...]
Iteration? [for, while, ...]. You also have to consider what you have
to work with
so far.

-- 
b h a a l u u at g m a i l dot c o m
http://www.geocities.com/ek.bhaaluu/index.html


No virus found in this outgoing message.
Checked by AVG Free Edition. 
Version: 7.5.487 / Virus Database: 269.13.25/1018 - Release Date: 9/19/2007
3:59 PM
 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Where can I find simple python scripts to edit and play around?

2007-09-26 Thread Ulrich Holtzhausen
Well I guess the subject/topic here describes it all. I am looking for a 
nice collection of SIMPLE python scripts, IE: How to manipulate files, 
text, how to create a bot, how to work with socks...making a 
server/client, converting decimal to binary etc. etc. etc. Example 
scripts since I am having a difficulty creating these myself since I am 
struggling to adapt to using creativity at the moment. I know that 
creativity is everything in programming. I just lack some experience + 
knowledge (examples) of how things are done. If someone knows of some 
sites or collections of these kinds of things from where I can 
view/download code from (possible) real world example 
applications/scripts that would be great. I want to modify and play 
around and read/interpret/understand what I'm struggling to conjour.

Thanks for any help and/or suggestions :)

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Where can I find simple python scripts to edit and play around?

2007-09-26 Thread bhaaluu
http://examples.oreilly.com/python3/
http://aspn.activestate.com/ASPN/Cookbook/Python/
http://examples.oreilly.com/twistedadn/
http://www.vex.net/parnassus/

I guess you could start with those and see what you can find?
-- 
b h a a l u u at g m a i l dot c o m
http://www.geocities.com/ek.bhaaluu/index.html

On 9/26/07, Ulrich Holtzhausen <[EMAIL PROTECTED]> wrote:
> Well I guess the subject/topic here describes it all. I am looking for a
> nice collection of SIMPLE python scripts, IE: How to manipulate files,
> text, how to create a bot, how to work with socks...making a
> server/client, converting decimal to binary etc. etc. etc. Example
> scripts since I am having a difficulty creating these myself since I am
> struggling to adapt to using creativity at the moment. I know that
> creativity is everything in programming. I just lack some experience +
> knowledge (examples) of how things are done. If someone knows of some
> sites or collections of these kinds of things from where I can
> view/download code from (possible) real world example
> applications/scripts that would be great. I want to modify and play
> around and read/interpret/understand what I'm struggling to conjour.
>
> Thanks for any help and/or suggestions :)
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Where can I find simple python scripts to edit and play around?

2007-09-26 Thread Andrew Nelsen
On 9/26/07, Ulrich Holtzhausen <[EMAIL PROTECTED]> wrote:
>
> Well I guess the subject/topic here describes it all. I am looking for a
> nice collection of SIMPLE python scripts, IE: How to manipulate files,
> text, how to create a bot, how to work with socks...making a
> server/client, converting decimal to binary etc. etc. etc. Example
> scripts since I am having a difficulty creating these myself since I am
> struggling to adapt to using creativity at the moment. I know that
> creativity is everything in programming. I just lack some experience +
> knowledge (examples) of how things are done. If someone knows of some
> sites or collections of these kinds of things from where I can
> view/download code from (possible) real world example
> applications/scripts that would be great. I want to modify and play
> around and read/interpret/understand what I'm struggling to conjour.
>
> Thanks for any help and/or suggestions :)


Check out this site: http://www.uselesspython.com It has some pretty nifty
stuff on there, but a lot of it is kinda old. Just my two cents - I'm pretty
new as well.

- Drew
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Where can I find simple python scripts to edit and play around?

2007-09-26 Thread Kent Johnson
Ulrich Holtzhausen wrote:
> Well I guess the subject/topic here describes it all. I am looking for a 
> nice collection of SIMPLE python scripts,

http://aspn.activestate.com/ASPN/Cookbook/Python as well as the printed 
version already referenced.
http://diveintopython.org/ has fairly long examples
http://www.amazon.com/Python-Phrasebook-Developers-Library-Dayley/dp/0672329107

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] do i have to import modules at the start of a file?

2007-09-26 Thread shawn bright
lo there all,

i have a gui program that imports a few modules that i don't need if i am
using the program remotely.
The program has an admin interface and thread interface, only the admin
interface is needed when
i am using the program from a remote computer.

all of my modules are imported at the start of the file, and not initiated
till a certain button is clicked on the GUI, could i put those
import statements in the button click event?

thanks

shawn
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] do i have to import modules at the start of a file?

2007-09-26 Thread Kent Johnson
shawn bright wrote:
> lo there all,
> 
> i have a gui program that imports a few modules that i don't need if i 
> am using the program remotely.
> The program has an admin interface and thread interface, only the admin 
> interface is needed when
> i am using the program from a remote computer.
> 
> all of my modules are imported at the start of the file, and not 
> initiated till a certain button is clicked on the GUI, could i put those
> import statements in the button click event?

Yes but why do you want to? The reasons I know for doing this:
- the import takes a long time or a lot of memory and you don't want to 
incur the cost unless you need it
- the imported module may not be available always and you want the rest 
of the importing module still to be usable

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] still need help..

2007-09-26 Thread Chris


Here some more work:


guess = 0

Print ?Pick a number between 0 and 100?


While guess !=  :
print ?My first guess is ?, guess
print ?Is my guess correct??
if guess = = type3
print ?I got it!?   
if guess > ___:
pick lower #
if guess <___ :
pick higher #

that?s all I can do right now?

the mechanism that I can?t figure out is how to show in code form how the
computer goes to the next guess using the split range... ie. if the first
guess is 50, the second guess would be taken from the 50-100 (50/2 = 25 + 50
= 75) second guess  would be 75 if the number is too high.  If the number
falls below 50, then the split range is 25, ect?

Internal Virus Database is out-of-date.
Checked by AVG Free Edition. 
Version: 7.5.487 / Virus Database: 269.13.25/1018 - Release Date: 9/19/2007
3:59 PM
 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] still need help..

2007-09-26 Thread Kent Johnson
Chris wrote:
> 
> Here some more work:
> 
> 
> guess = 0
> 
> Print ?Pick a number between 0 and 100?
> 
> 
> While guess !=  :
>   print ?My first guess is ?, guess
>   print ?Is my guess correct??
>   if guess = = type3
>   print ?I got it!?   
>   if guess > ___:
>   pick lower #
>   if guess <___ :
>   pick higher #
> 
> that?s all I can do right now?
> 
> the mechanism that I can?t figure out is how to show in code form how the
> computer goes to the next guess using the split range... ie. if the first
> guess is 50, the second guess would be taken from the 50-100 (50/2 = 25 + 50
> = 75) second guess  would be 75 if the number is too high.  If the number
> falls below 50, then the split range is 25, ect?

What I would do is keep a variable delta that gets added to or 
subtracted from the current guess, depending on whether the guess is too 
high or too low. Each time through the loop, make delta smaller by 
dividing by two unless delta is already down to 1.

Are you reading Python Programming for the absolute beginner? This 
problem is exercise 4 for chapter 3 in my copy. Have you tried exercises 
1-3? They are not so hard as this one.

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] i am stuck...

2007-09-26 Thread Chris
I don't know what to do at this point.  I do not have the book, but will get
it just to have...

Can anyone help me?

> Here some more work:
> 
> 
> guess = 0
> 
> Print ?Pick a number between 0 and 100?
> 
> 
> While guess !=  :
>   print ?My first guess is ?, guess
>   print ?Is my guess correct??
>   if guess = = type3
>   print ?I got it!?   
>   if guess > ___:
>   pick lower #
>   if guess <___ :
>   pick higher #
> 
> that?s all I can do right now?
> 
> the mechanism that I can?t figure out is how to show in code form how the
> computer goes to the next guess using the split range... ie. if the first
> guess is 50, the second guess would be taken from the 50-100 (50/2 = 25 +
50 = 75) second guess  would be 75 if the number is too high.  If the number
> falls below 50, then the split range is 25, ect?
 

Internal Virus Database is out-of-date.
Checked by AVG Free Edition. 
Version: 7.5.487 / Virus Database: 269.13.25/1018 - Release Date: 9/19/2007
3:59 PM
 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] i am stuck...

2007-09-26 Thread Ian Witham
On 9/27/07, Chris <[EMAIL PROTECTED]> wrote:
>
> I don't know what to do at this point.  I do not have the book, but will
> get
> it just to have...
>
> Can anyone help me?
>
> > Here some more work:
> >
> >
> > guess = 0
> >
> > Print ?Pick a number between 0 and 100?
> >
> >
> > While guess !=  :
> >   print ?My first guess is ?, guess
> >   print ?Is my guess correct??
> >   if guess = = type3
> >   print ?I got it!?
> >   if guess > ___:
> >   pick lower #
> >   if guess <___ :
> >   pick higher #
> >
> > that?s all I can do right now?
> >
> > the mechanism that I can?t figure out is how to show in code form how
> the
> > computer goes to the next guess using the split range... ie. if the
> first
> > guess is 50, the second guess would be taken from the 50-100 (50/2 = 25
> +
> 50 = 75) second guess  would be 75 if the number is too high.  If the
> number
> > falls below 50, then the split range is 25, ect?



Try having these 3 variables:

top, the top number that the secret number might be.
bottom, the lowest number that the secret number might be.
guess, the current guess.

if the guess is too high, then the value for guess should become your new
'top':
if guess > secret_number:
top = guess

If the guess is too low, do the opposite. 'bottom' should now be set to
guess

Now create a new value for guess using your new values for top and bottom,
and guess again.

Ian.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] i am stuck...

2007-09-26 Thread Ian Witham
On 9/27/07, Ian Witham <[EMAIL PROTECTED]> wrote:
>
> On 9/27/07, Chris <[EMAIL PROTECTED]> wrote:
> >
> > I don't know what to do at this point.  I do not have the book, but will
> > get
> > it just to have...
> >
> > Can anyone help me?
> >
> > > Here some more work:
> > >
> > >
> > > guess = 0
> > >
> > > Print ?Pick a number between 0 and 100?
> > >
> > >
> > > While guess !=  :
> > >   print ?My first guess is ?, guess
> > >   print ?Is my guess correct??
> > >   if guess = = type3
> > >   print ?I got it!?
> > >   if guess > ___:
> > >   pick lower #
> > >   if guess <___ :
> > >   pick higher #
> > >
> > > that?s all I can do right now?
> > >
> > > the mechanism that I can?t figure out is how to show in code form how
> > the
> > > computer goes to the next guess using the split range... ie. if the
> > first
> > > guess is 50, the second guess would be taken from the 50-100 (50/2 =
> > 25 +
> > 50 = 75) second guess  would be 75 if the number is too high.  If the
> > number
> > > falls below 50, then the split range is 25, ect?
>
>
>
> Try having these 3 variables:
>
> top, the top number that the secret number might be.
> bottom, the lowest number that the secret number might be.
> guess, the current guess.
>
> if the guess is too high, then the value for guess should become your new
> 'top':
> if guess > secret_number:
> top = guess
>
> If the guess is too low, do the opposite. 'bottom' should now be set to
> guess
>
> Now create a new value for guess using your new values for top and bottom,
> and guess again.
>
> Ian.
>

Some Pseudocode might help:

secret_number = something
bottom = 1
top = 101
guess = 50

while guess != secret number:

print "My guess is: ___"

if guess too low:
print "That guess is too low."
bottom = guess

if guess too high:
print "That guess is too high."
top = guess

guess = make a new guess, halfway between top and bottom

print "I have guessed your number, it is "


Ian
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor