Re: function that counts...

Discussion in 'Python' started by Raymond Hettinger, May 24, 2010.

  1. On May 19, 12:58 pm, superpollo <> wrote:
    > ... how many positive integers less than n have digits that sum up to m:
    >
    > In [197]: def prttn(m, n):
    >      tot = 0
    >      for i in range(n):
    >          s = str(i)
    >          sum = 0
    >          for j in range(len(s)):
    >              sum += int(s[j])
    >          if sum == m:
    >              tot += 1
    >      return tot
    >     .....:
    >
    > In [207]: prttn(25, 10000)
    > Out[207]: 348
    >
    > any suggestion for pythonizin' it?


    Your code is readable and does the job just fine.
    Not sure it is an improvement to reroll it into a one-liner:

    def prttn(m, n):
    return sum(m == sum(map(int, str(x))) for x in range(n))
    >>> prttn(25, 10000)

    348

    Some people find the functional style easier to verify because of
    fewer auxilliary variables and each step is testable independently.
    The m==sum() part is not very transparent because it relies on
    True==1, so it's only readable if it becomes a common idiom (which it
    could when you're answering questions of the form "how many numbers
    have property x").

    If speed is important, the global lookups can be localized:

    def prttn(m, n, map=itertools.imap, int=int, str=str, range=range):
    return sum(m == sum(map(int, str(x))) for x in range(n))

    Raymond
    Raymond Hettinger, May 24, 2010
    #1
    1. Advertising

  2. Raymond Hettinger

    Matteo Landi Guest

    What about avoiding the string conversion and use mod/div operations
    in order to create a list of digits for a number?

    Now that you have the sequences it's easy to count which sums up to m.

    On Mon, May 24, 2010 at 4:14 PM, Raymond Hettinger <> wrote:
    > On May 19, 12:58 pm, superpollo <> wrote:
    >> ... how many positive integers less than n have digits that sum up to m:
    >>
    >> In [197]: def prttn(m, n):
    >> tot = 0
    >> for i in range(n):
    >> s = str(i)
    >> sum = 0
    >> for j in range(len(s)):
    >> sum += int(s[j])
    >> if sum == m:
    >> tot += 1
    >> return tot
    >> .....:
    >>
    >> In [207]: prttn(25, 10000)
    >> Out[207]: 348
    >>
    >> any suggestion for pythonizin' it?

    >
    > Your code is readable and does the job just fine.
    > Not sure it is an improvement to reroll it into a one-liner:
    >
    > def prttn(m, n):
    > return sum(m == sum(map(int, str(x))) for x in range(n))
    >>>> prttn(25, 10000)

    > 348
    >
    > Some people find the functional style easier to verify because of
    > fewer auxilliary variables and each step is testable independently.
    > The m==sum() part is not very transparent because it relies on
    > True==1, so it's only readable if it becomes a common idiom (which it
    > could when you're answering questions of the form "how many numbers
    > have property x").
    >
    > If speed is important, the global lookups can be localized:
    >
    > def prttn(m, n, map=itertools.imap, int=int, str=str, range=range):
    > return sum(m == sum(map(int, str(x))) for x in range(n))
    >
    > Raymond
    >
    >
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >




    --
    Matteo Landi
    http://www.matteolandi.net/
    Matteo Landi, May 24, 2010
    #2
    1. Advertising

  3. Raymond Hettinger

    Bryan Guest

    Raymond Hettinger wrote:
    > If speed is important, the global lookups can be localized:
    >
    > def prttn(m, n, map=itertools.imap, int=int, str=str, range=range):
    >     return sum(m == sum(map(int, str(x))) for x in range(n))


    That's just silly. "If speed is important," we abandon the naive
    algorithm.


    --
    --Bryan
    Bryan, May 26, 2010
    #3
    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:
    237
    Victor Bazarov
    Feb 17, 2006
  2. Jerry Hill

    Re: function that counts...

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

    Re: function that counts...

    Steven D'Aprano, May 19, 2010, in forum: Python
    Replies:
    5
    Views:
    318
    Albert van der Horst
    May 21, 2010
  4. Peter Pearson

    Re: function that counts...

    Peter Pearson, May 20, 2010, in forum: Python
    Replies:
    3
    Views:
    247
    Bryan
    May 26, 2010
  5. Jean-Michel Pichavant

    Re: function that counts...

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

Share This Page