Velocity Reviews > Re: function that counts...

Re: function that counts...

Steven D'Aprano
Guest
Posts: n/a

 05-19-2010
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
Guest
Posts: n/a

 05-20-2010
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
Guest
Posts: n/a

 05-20-2010
On 2010-05-20, superpollo <(E-Mail Removed)> 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
Guest
Posts: n/a

 05-20-2010
On 2010-05-20, superpollo <(E-Mail Removed)> wrote:
> Grant Edwards ha scritto:
>> On 2010-05-20, superpollo <(E-Mail Removed)> 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

>>> 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
Guest
Posts: n/a

 05-21-2010
On Thu, 20 May 2010 19:53:42 +0000 (UTC)
Grant Edwards <(E-Mail Removed)> 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 <(E-Mail Removed)> | 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
Guest
Posts: n/a

 05-21-2010
In article <ht4406\$92c\$(E-Mail Removed)>,
Grant Edwards <(E-Mail Removed)> 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