Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Re: function that counts... (http://www.velocityreviews.com/forums/t723587-re-function-that-counts.html)

Steven D'Aprano 05-19-2010 09:51 PM

Re: function that counts...
 
On Wed, 19 May 2010 21:58:04 +0200, superpollo wrote:

> ... how many positive integers less than n have digits that sum up to m:
>
> In [197]: def prttn(m, n):


Does the name "prttn" mean anything? I'm afraid I keep reading it as a
mispelling of "print n".


[...]
> s = str(i)
> sum = 0
> for j in range(len(s)):
> sum += int(s[j])


Rather than iterating over an index j = 0, 1, 2, ... and then fetching
the jth character of the string, you can iterate over the characters
directly. So the inner loop is better written:

for c in s:
sum += int(c)




--
Steven

John Posner 05-20-2010 03:40 AM

Re: function that counts...
 
On 5/19/2010 5:51 PM, Steven D'Aprano wrote:
> On Wed, 19 May 2010 21:58:04 +0200, superpollo wrote:
>
>
> Rather than iterating over an index j = 0, 1, 2, ... and then fetching
> the jth character of the string, you can iterate over the characters
> directly. So the inner loop is better written:
>
> for c in s:
> sum += int(c)
>


Or, as a one-liner (and not shadowing the built-in *sum* function):

mysum = sum(map(int, str(i)))

-John

Grant Edwards 05-20-2010 04:39 PM

Re: function that counts...
 
On 2010-05-20, superpollo <utente@esempio.net> wrote:
> Steven D'Aprano ha scritto:
>> On Wed, 19 May 2010 21:58:04 +0200, superpollo wrote:
>>
>>> ... how many positive integers less than n have digits that sum up to m:
>>>
>>> In [197]: def prttn(m, n):

>>
>> Does the name "prttn" mean anything? I'm afraid I keep reading it as a
>> mispelling of "print n".

>
> pArtItIOn


One might be tempted to suggest the name "partition".

This isn't Fortran-IV. ;)

--
Grant Edwards grant.b.edwards Yow! I've got a COUSIN
at who works in the GARMENT
gmail.com DISTRICT ...

Grant Edwards 05-20-2010 07:53 PM

Re: function that counts...
 
On 2010-05-20, superpollo <utente@esempio.net> wrote:
> Grant Edwards ha scritto:
>> On 2010-05-20, superpollo <utente@esempio.net> wrote:
>>> Steven D'Aprano ha scritto:
>>>> On Wed, 19 May 2010 21:58:04 +0200, superpollo wrote:
>>>>
>>>>> ... how many positive integers less than n have digits that sum up to m:
>>>>
>>>> Does the name "prttn" mean anything? I'm afraid I keep reading it as
>>>> a mispelling of "print n".
>>>
>>> pArtItIOn

>>
>> One might be tempted to suggest the name "partition".

>
> no kidding: i was afraid to use some reserved word...


Since Python is interactive, and you don't get charged for each time
you run your deck through the reader, that's easy enough to check:

$ python
Python 2.6.4 (r264:75706, Mar 1 2010, 10:33:43)
[GCC 4.1.2 (Gentoo 4.1.2 p1.1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

>>> partition

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'partition' is not defined

>>> sum

<built-in function sum>

>>> int

<type 'int'>

>>> file

<type 'file'>


Lest my allusions to Fortran IV be lost upon the less grizzled, only
the first 6 characters were significant in Fortran IV identifiers, and
removing all of the vowels from a longer word was an idiomatic way to
create an identifier with a length <= 6.

IIRC, the number 6 was originally chosen because that's how many 6-bit
characters you could hold in a single 36-bit CPU register. That way
when writing a compiler/link/assembly you could compare two
identifiers using a single "CMP" instruction.

I'm not sure why 36-bits was such a popular ALU width, but many
different vendors sold 36-bit machines back in the day.

--
Grant Edwards grant.b.edwards Yow! Hand me a pair of
at leather pants and a CASIO
gmail.com keyboard -- I'm living
for today!

D'Arcy J.M. Cain 05-21-2010 02:11 AM

Re: function that counts...
 
On Thu, 20 May 2010 19:53:42 +0000 (UTC)
Grant Edwards <invalid@invalid.invalid> wrote:
> Since Python is interactive, and you don't get charged for each time
> you run your deck through the reader, that's easy enough to check:


Whoa! For a moment there I thought it was 1969. :-)

--
D'Arcy J.M. Cain <darcy@druid.net> | Democracy is three wolves
http://www.druid.net/darcy/ | and a sheep voting on
+1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.

Albert van der Horst 05-21-2010 04:02 PM

Re: function that counts...
 
In article <ht4406$92c$1@reader1.panix.com>,
Grant Edwards <invalid@invalid.invalid> wrote:
>
>Lest my allusions to Fortran IV be lost upon the less grizzled, only
>the first 6 characters were significant in Fortran IV identifiers, and
>removing all of the vowels from a longer word was an idiomatic way to
>create an identifier with a length <= 6.
>
>IIRC, the number 6 was originally chosen because that's how many 6-bit
>characters you could hold in a single 36-bit CPU register. That way
>when writing a compiler/link/assembly you could compare two
>identifiers using a single "CMP" instruction.
>
>I'm not sure why 36-bits was such a popular ALU width, but many
>different vendors sold 36-bit machines back in the day.


16 bit mini computers were popular: the pdp11.
Now 3 FORTRAN char's fitted in one word (radix 40).
At least it helped to shoe horn identifier names into two words.

>
>--
>Grant Edwards grant.b.edwards Yow! Hand me a pair of
> at leather pants and a CASIO
> gmail.com keyboard -- I'm living
> for today!



--
--
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- being exponential -- ultimately falters.
albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst



All times are GMT. The time now is 09:58 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.