[Tutor] need futher explaining

2007-08-06 Thread Dale Pearl
I'm reading Beginning Python - From Novice to Professional by Magnus Lie
Hetland (an Apress book) and there is a code example that I need further
explaining on to fully grasp.
There is a section with samle code of:
 names = ['anne', 'beth', 'george', 'damon']
ages = [12, 45, 32, 102]
for i in range(len(names)):
print names[i], 'is', ages[i], 'years old'

now all of it makes sense to me except for the line for i in
range(len(names)):
the len statement calculates the number of characters I'm not quite
understanding the magic here.
forgive my stupidity this programming stuff is new to me but if someone
could explain to me how this single line works it would be greatly
appreciated.

By the way this is a great book well worth the investment to anyone's
library who is trying to learn Python.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Batch file copy script

2007-08-06 Thread Luke Paireepinart
Majid wrote:
> Hi all,
> I am new to Python and this mailing list. I wondered if someone would be 
> kind enough to give me a basic python skeleton to achieve the following.
>
> Many thanks in advance
> Majid
>
> -
>
>
>   Here are the assumptions:
>
> 1. we have a list.txt file with entries like:
>
> /dists/dapper/Release.gpg
> /dists/dapper/main/binary-i386/Release
> /dists/dapper/restricted/binary-i386/Release
> /doc/install/manual/example-preseed.txt.gz
> /doc/install/manual/en/apa.html
> /doc/install/manual/en/apas01.html
>
> 2. The files referenced in the list file (and many other files) are
> in C:\source\ directory and subdirectories as defined in the list
> file - e.g. for the first entry above we have this file:
>
> C:\source\dists\dapper\Release.gpg
>
> 3. We want only the files listed in the list file to be copied to
> D:\target\ keeping the same directory structure. So for the first
> entry we want to have this file:
>
> D:\target\dists\dapper\Release.gpg
>
> 4. If a file listed in the list file is not found in the source, we
> want an entry to be added to C:\py\missing.txt (by the way the
> python script doing the job could be in this directory too - i.e.
> C:py\packagemaker.py - also the list file is here too: C:\py\list.txt)
>
>
>   Here is a possible logic for the script:
>
> 1. Open the list (C:\py\list.txt).
> 2. Until the end of the file
>3. Read a line from list.txt (e.g. '/dists/dapper/Release.gpg')
>4. Change forward slashes to back slashes (e.g.
> '\dists\dapper\Release.gpg')
>5. Add 'C:\source' to the string  (e.g.
> 'C:\source\dists\dapper\Release.gpg')
>6. If the file exists in the source folder
>   7. Copy the file from the source frolder to the target folder
> (e.g. copy C:\source\dists\dapper\Release.gpg to
> D:\target\dists\dapper\Release.gpg)
>8. Else
>   9. Add the name of the missing file to missings.txt
> 10. End of loop
>   
import os, shutil
target_dir = r'D:\target\'
source_dir = r'C:\source\'
missing_files = open("missings.txt","w")
for line in open('list.txt'):
source_file = os.path.normpath(source_dir + line)
target_file = os.path.normpath(target_dir + line)  
if os.path.exists(target_file):
   shutil.copyfile(source_file, target_file)
else:
missing_files.write(line)
missing_files.close()
  
It's pretty straight-forward.
Is anything in there confusing?
Things that you might not have seen before:
the r in front of the string makes it a 'raw string' meaning that a \t 
is a "\" and then a "t" instead of a tab character, and this goes for 
all other \s.
In other words, the '\' isn't used to escape certain special ascii 
sequences, it's just a '\'.

Secondly, the os.path.normpath() function will fix your path for your OS.
SO if you have
C:/some/path/to/file.ext
it will switch it to
C:\\some\\path\\to\\file.ext
if you're on Windows.
A good feature of this function is that it also fixes multiple slashes, so
C:\\source\\/dapper
will be correctly changed to
C:\\source\\dapper

Pretty cool function that I didn't know about 'til now.
>
>   Possible uses of the script?
>   
Not sure, you could probably generalize it quite a bit more than I did, 
if you found other use cases.
> I have copied the contents of an ubuntu dvd to my hard drive 
> (C:\source\). It contains many files and directories totaling 3.45 GB. I 
> have also downloaded the .list files for the server and alternate instal 
> iso images:
>
> ubuntu-6.06.1-server-i386.list and ubuntu-6.06.1-alternate-i386.list
>
> The ordinary 'desktop' install cd/dvd requires at least 512 MB or ram 
> which I don't have. It is also impossible to download the 700 MB iso 
> images for the server / alternate CDs as I only have a dial-up 
> connection. To make the situation worse, these CDs could not be found in 
> local stores in my city. So the only option left is making the server 
> and alternate CDs myself. This is why I need the script.
>   
That sucks.  It was sounding kind of like a homework problem at first, 
but I believe you.
I hope I helped,
if you have any other questions please ask!
-Luke
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Batch file copy script

2007-08-06 Thread bhaaluu
Greetings Majid,

https://shipit.ubuntu.com/

The Ubunty people will ship a set of 5 CDs of the latest
release to you, absolutely free. Go to the above site and
fill out your shipping information. I have done this in the
past, and they ship promptly. Why 5 CDs? It costs the
same to ship 1 or 5, and they'd like for you to share them
with other people.

Another source of CDs: do you have a Linux Users Group (LUG)
in your area? Also: look for Linux magazines with CD/DVD in
a local newstand. BTW, I only have 256MB RAM, and have never
had any problems installing Linux (Ubuntu or otherwise). Really,
all you should need to run/install a LiveCD is 128MB RAM, and if
you're not going to be running OpenOffice, you can get by with
less than that.

Hopefully helpful.
-- 
bhaaluu at gmail dot com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] need futher explaining

2007-08-06 Thread bhaaluu
Greetings,

I'm also a beginner to Python, but I think I can answer
your question. One of the best ways to learn about how
anything in Python works is to use the Python interactive
interpreter, so, away we go (follow along, please):

>>> names = ['anne', 'beth', 'george', 'damon']
>>> print names
['anne', 'beth', 'george', 'damon']
>>> print len(names)
4
>>> print names[0]
anne
>>> print names[3]
damon

1. names is a 'list' which contains four elements
2. The elements in a list are indexed starting with zero (0)
3. So the 'for' loop is iterating the length of the names list len(names)
   which is the same as saying:   for i in range(4):

So len() isn't just for counting characters! It's count will depend
on what 'type' it is counting. In the above case, it is counting elements
in a 'list'.

>>> print len(names[2])
6

names[2] is: g e o r g e
6 characters.
Why?

>>> print type(names[2])


george is a string, so len() counts the characters in the string.

I hope this is helpful.
-- 
bhaaluu at gmail dot com


On 8/6/07, Dale Pearl <[EMAIL PROTECTED]> wrote:
> I'm reading Beginning Python - From Novice to Professional by Magnus Lie
> Hetland (an Apress book) and there is a code example that I need further
> explaining on to fully grasp.
> There is a section with samle code of:
>   names = ['anne', 'beth', 'george', 'damon']
> ages = [12, 45, 32, 102]
> for i in range(len(names)):
> print names[i], 'is', ages[i], 'years old'
>
> now all of it makes sense to me except for the line for i in
> range(len(names)):
> the len statement calculates the number of characters I'm not quite
> understanding the magic here.
> forgive my stupidity this programming stuff is new to me but if someone
> could explain to me how this single line works it would be greatly
> appreciated.
>
> By the way this is a great book well worth the investment to anyone's
> library who is trying to learn Python.
>
> ___
> 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] Batch file copy script

2007-08-06 Thread Kent Johnson
Majid wrote:
>   Here is a possible logic for the script:
> 
> 1. Open the list (C:\py\list.txt).
> 2. Until the end of the file
>3. Read a line from list.txt (e.g. '/dists/dapper/Release.gpg')
>4. Change forward slashes to back slashes (e.g.
> '\dists\dapper\Release.gpg')
>5. Add 'C:\source' to the string  (e.g.
> 'C:\source\dists\dapper\Release.gpg')
>6. If the file exists in the source folder
>   7. Copy the file from the source frolder to the target folder
> (e.g. copy C:\source\dists\dapper\Release.gpg to
> D:\target\dists\dapper\Release.gpg)
>8. Else
>   9. Add the name of the missing file to missings.txt
> 10. End of loop

This would be a good first Python project. The actual script will be 
almost line-for-line the same as your outline. There are many good 
tutorials here:
http://wiki.python.org/moin/BeginnersGuide/NonProgrammers

You need to learn about lists and files, that's it. The shutil module 
has a copyfile() function that will do the actual copying.

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


Re: [Tutor] need futher explaining

2007-08-06 Thread Kent Johnson
Dale Pearl wrote:
> I'm reading Beginning Python - From Novice to Professional by Magnus Lie 
> Hetland (an Apress book) and there is a code example that I need further 
> explaining on to fully grasp.
> There is a section with samle code of:
>  names = ['anne', 'beth', 'george', 'damon']
> ages = [12, 45, 32, 102]
> for i in range(len(names)):
> print names[i], 'is', ages[i], 'years old'

bhaaluu answered your question, I just want to comment that IMO this is 
not idiomatic Python. A better way to write it would be to combine the 
two lists using zip():

for name, age in zip(names, ages):
   print name, 'is', age, 'years old'

zip: http://docs.python.org/lib/built-in-funcs.html#l2h-81

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


[Tutor] Ingenious script (IMO)

2007-08-06 Thread Dick Moores
Google Answers folded, but Google has kept the archive accessible. I 
found this Python script at 


and modified it for U.S. money denominations:

http://www.rcblue.com/Python/changeMaker_revised_for_US_denominations.py

I'm still working at Python--been at it a while--and thought the 
script was ingenious. Do the Tutors agree? Or is it just 
run-of-the-mill programming? Could it have been more simply written?

Thanks,

Dick Moores

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


Re: [Tutor] Ingenious script (IMO)

2007-08-06 Thread Kent Johnson
Dick Moores wrote:
> http://www.rcblue.com/Python/changeMaker_revised_for_US_denominations.py
> 
> I'm still working at Python--been at it a while--and thought the 
> script was ingenious. Do the Tutors agree? Or is it just 
> run-of-the-mill programming? Could it have been more simply written?

I don't find it either ingenious or well-written. The algorithm is the 
same one you would use to count out an amount by hand so it doesn't seem 
so unusual. IMO the code relies too much on indices. If you rewrite it 
using a dict (or, even better, defaultdict(int)) for coinCount, there is 
no need for indices at all. Even with coinCount as a list, it could be 
better written.

coinCount = []
for d in denominations:
 coinCount.append(0)

=>

coinCount = [0] * ndenominations


The main loop could be
for deno in range(ndenominations):
   if not remaining:
 break

or
for i, deno in enumerate(denominations):
# i is now the index, deno is the actual denomination


but I would write it this way and avoid the use of the index completely:

from collections import defaultdict
coinCount = defaultdict(int)
remaining = change

#   Loop until either we have given all the change or we have
#   run out of coin denominations to check.
for deno in denominations:
 if not remaining:
 break

 #   For one denomination, count out coins of that denomination
 #   as long as the remaining amount is greater than the denomination
 #   amount.

 while remaining >= deno:
 coinCount[deno] += 1
 print "remaining =", remaining
 remaining -= deno

#   Report the results.
print "Your change is $%.02f"% (float(change) / CPD)
for deno in denominations:
 if coinCount[deno] > 0:
 if deno >= 100:
 print "$%d bills:\t" % (deno / CPD), coinCount[deno]
 else:
 print "%d-cent coins:\t" % (deno), coinCount[deno]


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


Re: [Tutor] Ingenious script (IMO)

2007-08-06 Thread Dick Moores
At 06:49 AM 8/6/2007, Kent Johnson wrote:
>Dick Moores wrote:
>>http://www.rcblue.com/Python/changeMaker_revised_for_US_denominations.py
>>I'm still working at Python--been at it a while--and thought the 
>>script was ingenious. Do the Tutors agree? Or is it just 
>>run-of-the-mill programming? Could it have been more simply written?
>
>I don't find it either ingenious or well-written. The algorithm is 
>the same one you would use to count out an amount by hand so it 
>doesn't seem so unusual. IMO the code relies too much on indices. If 
>you rewrite it using a dict (or, even better, defaultdict(int)) for 
>coinCount, there is no need for indices at all. Even with coinCount 
>as a list, it could be better written.
>
>coinCount = []
>for d in denominations:
> coinCount.append(0)
>
>=>
>
>coinCount = [0] * ndenominations
>
>
>The main loop could be
>for deno in range(ndenominations):
>   if not remaining:
> break
>
>or
>for i, deno in enumerate(denominations):
># i is now the index, deno is the actual denomination
>
>
>but I would write it this way and avoid the use of the index completely:
>
>from collections import defaultdict
>coinCount = defaultdict(int)
>remaining = change
>
>#   Loop until either we have given all the change or we have
>#   run out of coin denominations to check.
>for deno in denominations:
> if not remaining:
> break
>
> #   For one denomination, count out coins of that denomination
> #   as long as the remaining amount is greater than the denomination
> #   amount.
>
> while remaining >= deno:
> coinCount[deno] += 1
> print "remaining =", remaining
> remaining -= deno
>
>#   Report the results.
>print "Your change is $%.02f"% (float(change) / CPD)
>for deno in denominations:
> if coinCount[deno] > 0:
> if deno >= 100:
> print "$%d bills:\t" % (deno / CPD), coinCount[deno]
> else:
> print "%d-cent coins:\t" % (deno), coinCount[deno]
>
>
>Kent

Thanks Kent!

Here's what I have after incorporating your suggestions:

=
#!/usr/bin/env python
#coding=utf-8
from collections import defaultdict

CPD = 100
cost = int(CPD * float(raw_input("Enter the cost: ")))
tend = int(CPD * float(raw_input("Enter the tendered amount: ")))

#   Calculate the change.
change = tend - cost

coinCount = defaultdict(int)
print coinCount
remaining = change
denominations = (2000, 1000, 500, 100, 50, 25, 10, 5, 1)

#   Loop until either we have given all the change or we have
#   run out of coin denominations to check.
for deno in denominations:
 if not remaining:
 break


 #   For one denomination, count out coins of that denomination
 #   as long as the remaining amount is greater than the denomination
 #   amount.


 while remaining >= deno:
 coinCount[deno] += 1
 remaining -= deno


#   Report the results.
print "Your change is $%.02f"% (float(change) / CPD)
for deno in denominations:
 if coinCount[deno] > 0:
 if deno >= 100:
 print "$%d bills:\t" % (deno / CPD), coinCount[deno]
 else:
 print "%d-cent coins:\t" % (deno), coinCount[deno]


Gotta say though, I still don't understand how the defaultdict works here.

Dick



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


Re: [Tutor] Ingenious script (IMO)

2007-08-06 Thread Kent Johnson
Dick Moores wrote:
> Gotta say though, I still don't understand how the defaultdict works here.

Did you try the docs?
http://docs.python.org/lib/defaultdict-objects.html

If coinCount were an ordinary dict, the line
   coinCount[deno] += 1

would have to be written as
   coinCount[deno] = coinCount.get(deno, 0) + 1

Using defaultdict(int) makes the use of 0 as the default value 
automatic. (Actually the default value is obtained by calling int(), 
whose value is 0.)

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


Re: [Tutor] sqlite: does "?" work in PRAGMA commands?

2007-08-06 Thread Terry Carroll
On Wed, 1 Aug 2007, Terry Carroll wrote:

> Does the "?" approach not work with PRAGMA commands or something; or am I
> doing this wrong?

Just a quick follow-up on this, in case anyone else cares.  My conclusion
is that it's not supported.  Googling around I found this pysqlite bug
report:

http://www.initd.org/tracker/pysqlite/ticket/160

It's not quite on target, since it's trying to use substitution for
non-SQL variables in an SQL statement, but I think the principle is the
same.  The PRAGMA statement is not really an SQL statement, so the
variables in it are not SQL variables.  So this is not-working as
designed, I think.

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


Re: [Tutor] Ingenious script (IMO)

2007-08-06 Thread Dick Moores
At 07:44 AM 8/6/2007, Kent Johnson wrote:
>Dick Moores wrote:
>>Gotta say though, I still don't understand how the defaultdict works here.
>
>Did you try the docs?
>http://docs.python.org/lib/defaultdict-objects.html

Yes, but it left me still in the dark.


>If coinCount were an ordinary dict, the line
>   coinCount[deno] += 1
>
>would have to be written as
>   coinCount[deno] = coinCount.get(deno, 0) + 1
>
>Using defaultdict(int) makes the use of 0 as the default value 
>automatic. (Actually the default value is obtained by calling int(), 
>whose value is 0.)

OK, thanks for spelling it out, Kent.

Dick

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


Re: [Tutor] Ingenious script (IMO)

2007-08-06 Thread Eric Brunson
Dick Moores wrote:
> At 06:49 AM 8/6/2007, Kent Johnson wrote:
>   
>> Dick Moores wrote:
>> 
>>> http://www.rcblue.com/Python/changeMaker_revised_for_US_denominations.py
>>> I'm still working at Python--been at it a while--and thought the 
>>> script was ingenious. Do the Tutors agree? Or is it just 
>>> run-of-the-mill programming? Could it have been more simply written?
>>>   
>> I don't find it either ingenious or well-written. The algorithm is 
>> the same one you would use to count out an amount by hand so it 
>> doesn't seem so unusual. IMO the code relies too much on indices. If 
>> you rewrite it using a dict (or, even better, defaultdict(int)) for 
>> coinCount, there is no need for indices at all. Even with coinCount 
>> as a list, it could be better written.
>>
>> coinCount = []
>> for d in denominations:
>> coinCount.append(0)
>>
>> =>
>>
>> coinCount = [0] * ndenominations
>>
>>
>> The main loop could be
>> for deno in range(ndenominations):
>>   if not remaining:
>> break
>>
>> or
>> for i, deno in enumerate(denominations):
>> # i is now the index, deno is the actual denomination
>>
>>
>> but I would write it this way and avoid the use of the index completely:
>>
>> 
> >from collections import defaultdict
>   
>> coinCount = defaultdict(int)
>> remaining = change
>>
>> #   Loop until either we have given all the change or we have
>> #   run out of coin denominations to check.
>> for deno in denominations:
>> if not remaining:
>> break
>>
>> #   For one denomination, count out coins of that denomination
>> #   as long as the remaining amount is greater than the denomination
>> #   amount.
>>
>> while remaining >= deno:
>> coinCount[deno] += 1
>> print "remaining =", remaining
>> remaining -= deno
>>
>> #   Report the results.
>> print "Your change is $%.02f"% (float(change) / CPD)
>> for deno in denominations:
>> if coinCount[deno] > 0:
>> if deno >= 100:
>> print "$%d bills:\t" % (deno / CPD), coinCount[deno]
>> else:
>> print "%d-cent coins:\t" % (deno), coinCount[deno]
>>
>>
>> Kent
>> 
>
> Thanks Kent!
>
> Here's what I have after incorporating your suggestions:
>
> =
> #!/usr/bin/env python
> #coding=utf-8
> from collections import defaultdict
>
> CPD = 100
> cost = int(CPD * float(raw_input("Enter the cost: ")))
> tend = int(CPD * float(raw_input("Enter the tendered amount: ")))
>
> #   Calculate the change.
> change = tend - cost
>
> coinCount = defaultdict(int)
> print coinCount
> remaining = change
> denominations = (2000, 1000, 500, 100, 50, 25, 10, 5, 1)
>
> #   Loop until either we have given all the change or we have
> #   run out of coin denominations to check.
> for deno in denominations:
>  if not remaining:
>  break
>
>
>  #   For one denomination, count out coins of that denomination
>  #   as long as the remaining amount is greater than the denomination
>  #   amount.
>
>
>  while remaining >= deno:
>  coinCount[deno] += 1
>  remaining -= deno
>
>   

Try something like:

def makechange( amount, denominations ):

coins = {}
for d in denominations:
coins[d] = int( amount/d )
amount = amount%d

return coins

> #   Report the results.
> print "Your change is $%.02f"% (float(change) / CPD)
> for deno in denominations:
>  if coinCount[deno] > 0:
>  if deno >= 100:
>  print "$%d bills:\t" % (deno / CPD), coinCount[deno]
>  else:
>  print "%d-cent coins:\t" % (deno), coinCount[deno]
> 
>
> Gotta say though, I still don't understand how the defaultdict works here.
>
> Dick
>
>
>
> ___
> 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] Ingenious script (IMO)

2007-08-06 Thread Dick Moores
On 8/6/07, Eric Brunson <[EMAIL PROTECTED]> wrote:
>
> Try something like:
>
> def makechange( amount, denominations ):
>
> coins = {}
> for d in denominations:
> coins[d] = int( amount/d )
> amount = amount%d
>
> return coins
>
> Sorry, but could you spell out your point?

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


Re: [Tutor] Ingenious script (IMO)

2007-08-06 Thread Eric Brunson

Multiple subtractions is called division.  It's a much more efficient loop.

In this version you have exactly as many iterations as denominations.  
It the original, if you wanted to know how many 200 coins are in 
10, you would iterate ~500 times.

Here's a timing test:

denominations = ( 200, 100, 50, 25, 10, 5, 1 )

def makechange( amount, denominations ):

coins = {}
for d in denominations:
coins[d] = int( amount/d )
amount = amount%d

return coins

def oldmakechange( amount, denominations ):

coins = {}
for d in denominations:
coins[d] = 0
while amount >= d:
coins[d] += 1
amount -= d

return coins

def comparetimings( trials=1, amount=1000 ):
from timeit import Timer
global denominations

new = Timer( "makechange( %s, denominations )" % amount, "from __main__ 
import makechange, denominations" ).timeit( trials )
old = Timer( "oldmakechange( %s, denominations )" % amount, "from __main__ 
import oldmakechange, denominations" ).timeit( trials )

print old, new, old/new


if __name__ == '__main__':
comparetimings()



Yields:  2.83604502678 0.000821828842163 3450.89498114

i.e. the division version runs about 3500 times faster for $100,000.

It's a minor thing, but you asked how we'd improve the code.  Math is a 
good thing to know.  ;-)

Dick Moores wrote:
> On 8/6/07, *Eric Brunson* <[EMAIL PROTECTED] 
>  > wrote:
>
> Try something like:
>
> def makechange( amount, denominations ):
>
> coins = {}
> for d in denominations:
> coins[d] = int( amount/d )
> amount = amount%d
>
> return coins
>
> Sorry, but could you spell out your point?
>
> Dick

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


Re: [Tutor] Ingenious script (IMO)

2007-08-06 Thread Dick Moores
At 10:16 AM 8/6/2007, Eric Brunson wrote:

Your point about efficiency is well-taken.

>def makechange( amount, denominations ):
>
> coins = {}
> for d in denominations:
> coins[d] = int( amount/d )
> amount = amount%d
>
> return coins

OK, I used this this way:


def makechange( amount, denominations ):

coins = {}
for d in denominations:
coins[d] = int( amount/d )
amount = amount%d

return coins

denominations = (2000, 1000, 500, 100, 50, 25, 10, 5, 1)
amount = 2218
print makechange(2218, denominations)
==

And get:
{1: 3, 100: 2, 5: 1, 1000: 0, 10: 1, 2000: 1, 50: 0, 500: 0, 25: 0}

That's the correct change: 3 pennies, 2 $1 bills, 1 nickel, no $10 
bills, 1 dime, 1 $20 bill, no half-dollars, no $5 bills, no quarters.

For amount = 3288:
{1: 3, 100: 2, 5: 0, 1000: 1, 10: 1, 2000: 1, 50: 1, 500: 0, 25: 1}

Why those weird orders?

Dick



==
   Bagdad Weather
 

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


Re: [Tutor] Ingenious script (IMO)

2007-08-06 Thread Kent Johnson
Dick Moores wrote:
> At 10:16 AM 8/6/2007, Eric Brunson wrote:
> 
> Your point about efficiency is well-taken.
> 
>> def makechange( amount, denominations ):
>>
>> coins = {}
>> for d in denominations:
>> coins[d] = int( amount/d )
>> amount = amount%d
>>
>> return coins
> 
> OK, I used this this way:
> 
> 
> def makechange( amount, denominations ):
> 
> coins = {}
> for d in denominations:
> coins[d] = int( amount/d )
> amount = amount%d
> 
> return coins
> 
> denominations = (2000, 1000, 500, 100, 50, 25, 10, 5, 1)
> amount = 2218
> print makechange(2218, denominations)
> ==
> 
> And get:
> {1: 3, 100: 2, 5: 1, 1000: 0, 10: 1, 2000: 1, 50: 0, 500: 0, 25: 0}
> 
> That's the correct change: 3 pennies, 2 $1 bills, 1 nickel, no $10 
> bills, 1 dime, 1 $20 bill, no half-dollars, no $5 bills, no quarters.
> 
> For amount = 3288:
> {1: 3, 100: 2, 5: 0, 1000: 1, 10: 1, 2000: 1, 50: 1, 500: 0, 25: 1}
> 
> Why those weird orders?

Dictionaries are not ordered. If you want to see it in order you could 
sort the list of key, value pairs:
print sorted(makechange(2218, denominations).items(), reverse=True)

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


[Tutor] superscript with easydialogs

2007-08-06 Thread Ben
Hi,

I have been working with easydialogs module lately especially the progress
bar (for Windows). I would like to put superscript text like (TM) to (™)
when calling the label function. I have been looking around the net for some
info, and can not find anything about it . It makes me wonder if python
itself allow to  the superscript.  Any suggestions? Thanks in advance.

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


Re: [Tutor] Ingenious script (IMO)

2007-08-06 Thread Tiger12506
Nice idea. Written style is average. Other tutors have discussed issues with 
performance, style, etc. I thought I would mention that whenever I am asked 
to give my opinion on a script, I compare it to something I have 
written/would write. In this case, I have already written. In my version, it 
not only tells how many of each denomination, but also how one would count 
back the change. It would be an interesting challenge to implement that, no? 
;-)

JS 

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


Re: [Tutor] Ingenious script (IMO)

2007-08-06 Thread Luke Paireepinart
Dick Moores wrote:
> At 10:16 AM 8/6/2007, Eric Brunson wrote:
>
> Your point about efficiency is well-taken.
>
>   
>> def makechange( amount, denominations ):
>>
>> coins = {}
>> for d in denominations:
>> coins[d] = int( amount/d )
>> amount = amount%d
>>
>> return coins
>> 
>
> OK, I used this this way:
>
> 
> def makechange( amount, denominations ):
>
> coins = {}
> for d in denominations:
> coins[d] = int( amount/d )
> amount = amount%d
>
> return coins
>
> denominations = (2000, 1000, 500, 100, 50, 25, 10, 5, 1)
> amount = 2218
> print makechange(2218, denominations)
> ==
>
> And get:
> {1: 3, 100: 2, 5: 1, 1000: 0, 10: 1, 2000: 1, 50: 0, 500: 0, 25: 0}
>
> That's the correct change: 3 pennies, 2 $1 bills, 1 nickel, no $10 
> bills, 1 dime, 1 $20 bill, no half-dollars, no $5 bills, no quarters.
>
> For amount = 3288:
> {1: 3, 100: 2, 5: 0, 1000: 1, 10: 1, 2000: 1, 50: 1, 500: 0, 25: 1}
>
> Why those weird orders?
>   
Dictionaries are implemented as hash tables, not linked lists or some 
other data structure.
see
> http://en.wikipedia.org/wiki/Hash_table
for more info.
a dictionary's keys may coincidentally be ordered how you'd expect them 
to be, but it's not required so you should never depend on that.
If you need them sorted, sort them :)
-Luke
>   

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


Re: [Tutor] Ingenious script (IMO)

2007-08-06 Thread Eric Brunson
Dick Moores wrote:
> At 10:16 AM 8/6/2007, Eric Brunson wrote:
>
> Your point about efficiency is well-taken.
>
>> def makechange( amount, denominations ):
>>
>> coins = {}
>> for d in denominations:
>> coins[d] = int( amount/d )
>> amount = amount%d
>>
>> return coins
>
> OK, I used this this way:
>
> 
> def makechange( amount, denominations ):
>
>coins = {}
>for d in denominations:
>coins[d] = int( amount/d )
>amount = amount%d
>
>return coins
>
> denominations = (2000, 1000, 500, 100, 50, 25, 10, 5, 1)
> amount = 2218
> print makechange(2218, denominations)
> ==
>
> And get:
> {1: 3, 100: 2, 5: 1, 1000: 0, 10: 1, 2000: 1, 50: 0, 500: 0, 25: 0}
>
> That's the correct change: 3 pennies, 2 $1 bills, 1 nickel, no $10 
> bills, 1 dime, 1 $20 bill, no half-dollars, no $5 bills, no quarters.
>
> For amount = 3288:
> {1: 3, 100: 2, 5: 0, 1000: 1, 10: 1, 2000: 1, 50: 1, 500: 0, 25: 1}
>
> Why those weird orders?

Dictionary keys are not guaranteed to return in the order you add them.

You can sort them manually if it's important:

change = makechange( 2218, denominations )
print [ ( k, change[k] ) for k in sorted( change ) ]

Or, since you have a sorted list of denominations already:

change = makechange( 2218, denominations)
print [ ( k, change[k] ) for k in denominations ) ]

Or, if you know you're going to want them sorted use:

coins = []
for d in denominations:
   coins.append( ( d, int( amount/d ) )
   amount = amount%d

to get an ordered list of tuples.


Isn't programming fun?



>
> Dick
>
>
>
> ==
>   Bagdad Weather
> 

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


Re: [Tutor] Ingenious script (IMO)

2007-08-06 Thread Ken Oliver


-Original Message-
>From: Kent Johnson <[EMAIL PROTECTED]>
>Sent: Aug 6, 2007 3:46 PM
>To: Dick Moores <[EMAIL PROTECTED]>
>Cc: Python Tutor List 
>Subject: Re: [Tutor] Ingenious script (IMO)
>
>Dick Moores wrote:
>> At 10:16 AM 8/6/2007, Eric Brunson wrote:
>> 
>> Your point about efficiency is well-taken.
>> 
>>> def makechange( amount, denominations ):
>>>
>>> coins = {}
>>> for d in denominations:
>>> coins[d] = int( amount/d )
>>> amount = amount%d
>>>
>>> return coins
>> 
>> OK, I used this this way:
>> 
>> 
>> def makechange( amount, denominations ):
>> 
>> coins = {}
>> for d in denominations:
>> coins[d] = int( amount/d )
>> amount = amount%d
>> 
>> return coins
>> 
>> denominations = (2000, 1000, 500, 100, 50, 25, 10, 5, 1)
>> amount = 2218
>> print makechange(2218, denominations)
>> ==
>> 
>> And get:
>> {1: 3, 100: 2, 5: 1, 1000: 0, 10: 1, 2000: 1, 50: 0, 500: 0, 25: 0}
>> 
>> That's the correct change: 3 pennies, 2 $1 bills, 1 nickel, no $10 
>> bills, 1 dime, 1 $20 bill, no half-dollars, no $5 bills, no quarters.
>> 
>> For amount = 3288:
>> {1: 3, 100: 2, 5: 0, 1000: 1, 10: 1, 2000: 1, 50: 1, 500: 0, 25: 1}
>> 
>> Why those weird orders?
>
>Dictionaries are not ordered. If you want to see it in order you could 
>sort the list of key, value pairs:
>print sorted(makechange(2218, denominations).items(), reverse=True)
>
>Kent

Do you have a clever, pythonic way to suppress the denominations with "zero" 
counts?

Ken

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


Re: [Tutor] Ingenious script (IMO)

2007-08-06 Thread Kent Johnson
Ken Oliver wrote:
> 
> -Original Message-
>> From: Kent Johnson <[EMAIL PROTECTED]>
>> Sent: Aug 6, 2007 3:46 PM
>> To: Dick Moores <[EMAIL PROTECTED]>
>> Cc: Python Tutor List 
>> Subject: Re: [Tutor] Ingenious script (IMO)
>>
>> Dick Moores wrote:
>>> At 10:16 AM 8/6/2007, Eric Brunson wrote:
>>>
>>> Your point about efficiency is well-taken.
>>>
 def makechange( amount, denominations ):

 coins = {}
 for d in denominations:
 coins[d] = int( amount/d )
 amount = amount%d

 return coins
>>> OK, I used this this way:
>>>
>>> 
>>> def makechange( amount, denominations ):
>>>
>>> coins = {}
>>> for d in denominations:
>>> coins[d] = int( amount/d )
>>> amount = amount%d
>>>
>>> return coins
>>>
>>> denominations = (2000, 1000, 500, 100, 50, 25, 10, 5, 1)
>>> amount = 2218
>>> print makechange(2218, denominations)
>>> ==
>>>
>>> And get:
>>> {1: 3, 100: 2, 5: 1, 1000: 0, 10: 1, 2000: 1, 50: 0, 500: 0, 25: 0}
>>>
>>> That's the correct change: 3 pennies, 2 $1 bills, 1 nickel, no $10 
>>> bills, 1 dime, 1 $20 bill, no half-dollars, no $5 bills, no quarters.
>>>
>>> For amount = 3288:
>>> {1: 3, 100: 2, 5: 0, 1000: 1, 10: 1, 2000: 1, 50: 1, 500: 0, 25: 1}
>>>
>>> Why those weird orders?
>> Dictionaries are not ordered. If you want to see it in order you could 
>> sort the list of key, value pairs:
>> print sorted(makechange(2218, denominations).items(), reverse=True)
>>
>> Kent
> 
> Do you have a clever, pythonic way to suppress the denominations with "zero" 
> counts?

Sure, just use a list comprehension to filter the list of items:

non_zero_items = [(k, v) for k, v in makechange(2218, 
denominations).items() if v!=0]
print sorted(non_zero_items, reverse=True)

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


Re: [Tutor] superscript with easydialogs

2007-08-06 Thread Alan Gauld
"Ben" <[EMAIL PROTECTED]> wrote

> I have been working with easydialogs module lately
> especially the progress bar (for Windows). I would
> like to put superscript text like (TM) to (™) when
> calling the label function.

To do that you will have to use rich text format for the label
text and that's not normally available in Windows widgets.

The super-scripting is controlled by the text style,
font settings etc. You may be able to find a font that
supports TM as a superscript specifically within its
extended charater set. But I dont know if EasyDialogs
even allows you to change the font...

> It makes me wonder if python itself allow to  the
> superscript.  Any suggestions? Thanks in advance.

Its not a Python issue but a matter of what the standard
windows widgets allow. To achieve what you want you might
have to create a custom widget when supports display of
rich text in its labels. But that would be true regardless
of the programming langusage you used!

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 


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


Re: [Tutor] superscript with easydialogs

2007-08-06 Thread Terry Carroll
On Mon, 6 Aug 2007, Alan Gauld wrote:

> "Ben" <[EMAIL PROTECTED]> wrote
> 
> > I have been working with easydialogs module lately
> > especially the progress bar (for Windows). I would
> > like to put superscript text like (TM) to (?) when
> > calling the label function.
> 
> The super-scripting is controlled by the text style,
> font settings etc. You may be able to find a font that
> supports TM as a superscript specifically within its
> extended charater set. But I dont know if EasyDialogs
> even allows you to change the font...

I don't know easydialogs, but could you just use Unicode?

label = u"SpamEateru\2122, now with EGG support"

where 2122 is the Unicode codepoint for the TM symbol.

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


Re: [Tutor] Ingenious script (IMO)

2007-08-06 Thread Kent Johnson
Ken Oliver wrote:
> I seem to stumble often with things like
> 
> nz = [ (k,v) for k,v in lst if v!=0]
> 
> I have not been able to wrap my brain around the parentheses. I see
> it
as reasonable to have the () around the k,v in the tuple in the first
instance, but I feel like they should also be around k,v in the second
instance (the for clause).

You can write it as [ (k,v) for (k,v) in lst if v!=0] if you prefer.

> Now can you do it all in one statement? Hehe. Just teasing.

I hope it's obvious how to do that...

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


Re: [Tutor] Ingenious script (IMO)

2007-08-06 Thread Dick Moores
At 02:17 PM 8/6/2007, you wrote:
>Nice idea. Written style is average. Other tutors have discussed issues with
>performance, style, etc. I thought I would mention that whenever I am asked
>to give my opinion on a script, I compare it to something I have
>written/would write. In this case, I have already written. In my version, it
>not only tells how many of each denomination, but also how one would count
>back the change. It would be an interesting challenge to implement that, no?
>;-)

First remind me how Americans do that. I lived in Japan a long time, 
and it's done quite differently there.

For the U.S., say the output so far is:

Enter the cost: 5.77
Enter the tendered amount: 10
Your change is $4.23
$1 bills:   4
10-cent coins:  2
1-cent coins:   3

What would be the U.S. way of counting back the change? I think we 
start with the $5.77, but then what?

Dick

==
   Bagdad Weather
 

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


Re: [Tutor] Ingenious script (IMO)

2007-08-06 Thread Alan Gauld

"Dick Moores" <[EMAIL PROTECTED]> wrote

> For the U.S., say the output so far is:
>
> Enter the cost: 5.77
> Enter the tendered amount: 10
> Your change is $4.23

> What would be the U.S. way of counting back the change? I think we
> start with the $5.77, but then what?

Dunno about the US but in the UK we generally just hand over the total
change as indicated on the till nowadays! :-)

But in days of yore it was done thusly:

5.77 and 3(cents) is 5.80 and 20 (cents)  is 6 and 4 (dollars) is 10.

Except of course we use pounds and pence!

Alan G. 


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


Re: [Tutor] Ingenious script (IMO)

2007-08-06 Thread Kent Johnson
Dick Moores wrote:
> At 02:17 PM 8/6/2007, you wrote:
>> Nice idea. Written style is average. Other tutors have discussed issues with
>> performance, style, etc. I thought I would mention that whenever I am asked
>> to give my opinion on a script, I compare it to something I have
>> written/would write. In this case, I have already written. In my version, it
>> not only tells how many of each denomination, but also how one would count
>> back the change. It would be an interesting challenge to implement that, no?
>> ;-)
> 
> First remind me how Americans do that. I lived in Japan a long time, 
> and it's done quite differently there.
> 
> For the U.S., say the output so far is:
> 
> Enter the cost: 5.77
> Enter the tendered amount: 10
> Your change is $4.23
> $1 bills:   4
> 10-cent coins:  2
> 1-cent coins:   3
> 
> What would be the U.S. way of counting back the change? I think we 
> start with the $5.77, but then what?

The traditional way starts from the cost and counts up to the amount 
tendered. So to make change from $10 for $5.77 you would count
three pennies -> $5.80
two dimes -> $6.00
four $1 -> $10

The modern way seems to be to look at the change amount given by the 
cash register and count that out starting with dollars...

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


Re: [Tutor] superscript with easydialogs

2007-08-06 Thread Alan Gauld

"Terry Carroll" <[EMAIL PROTECTED]> wrote

> > like to put superscript text like (TM) to (T) when
> > calling the label function.
> 
> font settings etc. You may be able to find a font that
> supports TM as a superscript specifically within its
> extended charater set. 
>
> I don't know easydialogs, but could you just use Unicode?
>
> label = u"SpamEateru\2122, now with EGG support"

That would be one instance of a font that supported 
the TM character. The problem is I don't know whether 
Windows supports unicode(I suspect it does nowadays)
and if it does whether EasyDialogs supports changing 
the system font.

If you don't mind messing about with the font used by 
the OS in all dialogs it might be possible that way...

Alan G.

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


Re: [Tutor] superscript with easydialogs

2007-08-06 Thread Terry Carroll
On Tue, 7 Aug 2007, Alan Gauld wrote:

> The problem is I don't know whether Windows supports unicode(I suspect
> it does nowadays) and if it does whether EasyDialogs supports changing
> the system font.

I think EasyDialogs is a Mac thing.



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


Re: [Tutor] superscript with easydialogs

2007-08-06 Thread Luke Paireepinart
Terry Carroll wrote:
> On Tue, 7 Aug 2007, Alan Gauld wrote:
>
>   
>> The problem is I don't know whether Windows supports unicode(I suspect
>> it does nowadays) and if it does whether EasyDialogs supports changing
>> the system font.
>> 
>
> I think EasyDialogs is a Mac thing.
>   
Ben mentioned Windows specifically in his original post.
I think that's why Alan was talking about it.
-Luke
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] superscript with easydialogs

2007-08-06 Thread Terry Carroll
On Mon, 6 Aug 2007, Luke Paireepinart wrote:

> Ben mentioned Windows specifically in his original post.
> I think that's why Alan was talking about it.

Ah.  I missed that, thanks.

Well, in that case, since I use Windows, I can give it a shot instead of 
guessing.

It turns out, no, Unicode won't work, but using x\99 for the TM character 
does, at least on my system (no idea if this will be universal):

import EasyDialogs
lim = 10
title = "FooBar\x99"
bar = EasyDialogs.ProgressBar(title, maxval=lim)
for i in range(lim):
bar.inc()
del bar



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


Re: [Tutor] Ingenious script (IMO)

2007-08-06 Thread Tiger12506
> The modern way seems to be to look at the change amount given by the
> cash register and count that out starting with dollars...
So true... tsk tsk.

That's because the teenagers that give you the change do not know how to 
count it back. What a great idea to write a program that can show them how! 
Or perhaps the excuse is more the truth - it's faster to throw the change at 
you. I know that many old-timers would be very impressed to have their 
change counted back to them. (It's required sometimes-my father told stories 
of a blind man that knew how much money he had and where in his wallet it 
was by how the cashier counted it back).

I imagine that however exactly it is phrased when you count back change is 
dialectual. Some people do it some way, some do it other ways. In my part of 
the US, the "proper" way is:

$10.00
Say "5.77"
"3 makes 80"
"20 makes 6"
"and four makes 10 dollars"
"Have a nice day"

While handing out the described amount at each line break. Sometimes, 
especially in important applications, like in a bank, they will count and 
hand the bills out to you individually - i.e. "and one, two, three, four 
makes 10 dollars"
Of course, the "have a nice day" is optional, but it makes a nice touch ;-)
Among the elders, it is considered very courteous to count back the change, 
but so often this is not the case that it is no longer considered rude to 
skip the counting...

Anyway, the python part of this discussion is to point out that the method 
varies, so it would be even more of a challenge to provide options for how 
the change should be counted.

JS 

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


Re: [Tutor] superscript with easydialogs

2007-08-06 Thread Tiger12506
> It turns out, no, Unicode won't work, but using x\99 for the TM character
> does, at least on my system (no idea if this will be universal):

That's strange. Windows is Unicode based! All text operations done in 
Windows are first converted to unicode, calculated, and then back. That's 
even been mentioned on this list... 

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


Re: [Tutor] Ingenious script (IMO)

2007-08-06 Thread Dick Moores
At 08:38 PM 8/6/2007, Tiger12506 wrote:
> > The modern way seems to be to look at the change amount given by the
> > cash register and count that out starting with dollars...
>So true... tsk tsk.
>
>That's because the teenagers that give you the change do not know how to
>count it back. What a great idea to write a program that can show them how!
>Or perhaps the excuse is more the truth - it's faster to throw the change at
>you. I know that many old-timers would be very impressed to have their
>change counted back to them. (It's required sometimes-my father told stories
>of a blind man that knew how much money he had and where in his wallet it
>was by how the cashier counted it back).
>
>I imagine that however exactly it is phrased when you count back change is
>dialectual. Some people do it some way, some do it other ways. In my part of
>the US, the "proper" way is:
>
>$10.00
>Say "5.77"
>"3 makes 80"
>"20 makes 6"
>"and four makes 10 dollars"
>"Have a nice day"
>
>While handing out the described amount at each line break. Sometimes,
>especially in important applications, like in a bank, they will count and
>hand the bills out to you individually - i.e. "and one, two, three, four
>makes 10 dollars"
>Of course, the "have a nice day" is optional, but it makes a nice touch ;-)
>Among the elders, it is considered very courteous to count back the change,
>but so often this is not the case that it is no longer considered rude to
>skip the counting...
>
>Anyway, the python part of this discussion is to point out that the method
>varies, so it would be even more of a challenge to provide options for how
>the change should be counted.

OK, I'll give it a try. Thanks for the challenges.

Dick

==
   Bagdad Weather
 

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