function that counts...
superpollo
utente at esempio.net
Wed May 19 16:58:22 EDT 2010
Jerry Hill ha scritto:
> On Wed, May 19, 2010 at 4:25 PM, superpollo <utente at esempio.net> wrote:
>> Jerry Hill ha scritto:
>>> 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 ?
>
> No, sum() has been a builtin since Python 2.3. Based on your first
> post, you have probably shadowed the builtin "sum" function by
> assigning an integer to a variable named "sum".
o my... thanks!
In [266]: del(sum)
In [267]: def prttn2(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))
if sumofdigits == m:
total += 1
return total
.....:
In [275]: def prttn3(m, n):
return sum(1 for x in range(n) if sum(map(int, str(x))) == m)
.....:
In [277]: prttn(25, 10000)
Out[277]: 348
In [278]: prttn2(25, 10000)
Out[278]: 348
In [279]: prttn3(25, 10000)
Out[279]: 348
ok, bye!
More information about the Python-list
mailing list