[Tutor] Ingenious script (IMO)

bob gailer bgailer at alum.rpi.edu
Sat Aug 11 17:28:01 CEST 2007


Dick Moores wrote:
> At 07:13 AM 8/11/2007, bob gailer wrote:
>   
>> Dick Moores wrote:
>>     
>>> Here's the script that hands over change in the traditional way. 
>>> <http://www.rcblue.com/Misc/changeMakerKent_Traditional.py>
>>>
>>> Is there a better way to handle the singulars and plurals?
>>>
>>>       
>> What do you mean by "better"?
>> More efficient?
>> More readable/maintainable?
>> Less code?
>>     
>
> Phythonic.
>   
OK, I'll take a stab.

  coins = ('penny', 'pennies', 'nickel', 'nickels', 'dime', 
           'dimes', 'quarter','quarters', 'half-dollar')

First I'd move that outside the loop, since it needs be assigned only once. Then I'd change it to:
  
  coins = (('penny', 'pennies'), ('nickel', 'nickels'), ('dime', 
            'dimes'), ('quarter','quarters'), ('half-dollar',))

Next I'd replace this

 if coinCount[deno] == 1:
      astr = coins[idx * 2]
  else:
      astr = coins[idx * 2 + 1]

with (using a more meaningful name than astr)

   coin = coins[idx][coinCount[deno] == 1]



I must stop here due to time constraints. When I read the entire program 
there's a lot I'd change to make it "better".



More information about the Tutor mailing list