Re: function that counts...

Discussion in 'Python' started by Steven D'Aprano, May 19, 2010.

  1. 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
     
    Steven D'Aprano, May 19, 2010
    #1
    1. Advertising

  2. Steven D'Aprano

    John Posner Guest

    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
     
    John Posner, May 20, 2010
    #2
    1. Advertising

  3. On 2010-05-20, superpollo <> 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, May 20, 2010
    #3
  4. On 2010-05-20, superpollo <> wrote:
    > Grant Edwards ha scritto:
    >> On 2010-05-20, superpollo <> 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!
     
    Grant Edwards, May 20, 2010
    #4
  5. On Thu, 20 May 2010 19:53:42 +0000 (UTC)
    Grant Edwards <> 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 <> | 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.
     
    D'Arcy J.M. Cain, May 21, 2010
    #5
  6. In article <ht4406$92c$>,
    Grant Edwards <> 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
     
    Albert van der Horst, May 21, 2010
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. utab
    Replies:
    3
    Views:
    240
    Victor Bazarov
    Feb 17, 2006
  2. Jerry Hill

    Re: function that counts...

    Jerry Hill, May 19, 2010, in forum: Python
    Replies:
    2
    Views:
    275
    Steven D'Aprano
    May 19, 2010
  3. Peter Pearson

    Re: function that counts...

    Peter Pearson, May 20, 2010, in forum: Python
    Replies:
    3
    Views:
    255
    Bryan
    May 26, 2010
  4. Raymond Hettinger

    Re: function that counts...

    Raymond Hettinger, May 24, 2010, in forum: Python
    Replies:
    2
    Views:
    306
    Bryan
    May 26, 2010
  5. Jean-Michel Pichavant

    Re: function that counts...

    Jean-Michel Pichavant, May 24, 2010, in forum: Python
    Replies:
    7
    Views:
    310
    Bryan
    Jun 11, 2010
Loading...

Share This Page