function that counts...
superpollo
utente at esempio.net
Wed May 19 16:25:31 EDT 2010
Jerry Hill ha scritto:
> On Wed, May 19, 2010 at 3:58 PM, superpollo <utente at esempio.net> wrote:
>> ... how many positive integers less than n have digits that sum up to m:
> ...
>> any suggestion for pythonizin' it?
>
> This is how I would do it:
>
> def prttn(m, n):
> """How many positive integers less than n have digits that sum up to m"""
> total = 0
> for testval in range(n):
> sumofdigits = sum(int(char) for char in str(testval))
this line gives me this:
TypeError: 'int' object is not callable
is it some new feature in >2.5 ?
> if sumofdigits == m:
> total += 1
> return total
>
> I added a docstring to the function, saying what it does, and what the
> arguments are supposed to represent. I also moved the
> convert-to-string-and-sum-the-digits logic into a single generator
> expression that's passed to the builtin sum function. Oh, and I tried
> to use slightly more expressive variable names.
>
More information about the Python-list
mailing list