From: Jerry Hill on
On Wed, May 19, 2010 at 4:25 PM, superpollo <utente(a)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".

--
Jerry
From: Mark Dickinson on
On May 19, 9:30 pm, superpollo <ute...(a)esempio.net> wrote:
> René 'Necoro' Neumann ha scritto:
> > An idea would be:
>
> >>>> def prttn(m, n):
> > ...        return sum(1 for x in range(n) if sum(map(int, str(x))) == m)
>
> TypeError: 'int' object is not callable
>
> on 2.5.4

The TypeError is almost certainly because you've created a integer
'sum' variable in your script/interpreter session, hiding the built-in
sum function.

--
Mark
From: superpollo on
Jerry Hill ha scritto:
> On Wed, May 19, 2010 at 4:25 PM, superpollo <utente(a)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!
From: superpollo on
Mark Dickinson ha scritto:
> On May 19, 9:30 pm, superpollo <ute...(a)esempio.net> wrote:
>> Ren� 'Necoro' Neumann ha scritto:
>>> An idea would be:
>>>>>> def prttn(m, n):
>>> ... return sum(1 for x in range(n) if sum(map(int, str(x))) == m)
>> TypeError: 'int' object is not callable
>>
>> on 2.5.4
>
> The TypeError is almost certainly because you've created a integer
> 'sum' variable in your script/interpreter session, hiding the built-in
> sum function.

thx
From: René 'Necoro' Neumann on
Am 19.05.2010 22:58, schrieb superpollo:
>
> 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!

Just because I was curios:

necoro(a)Zakarumiy ~ % python -m timeit "import test; test.prttn(25,10000)"
10 loops, best of 3: 108 msec per loop

necoro(a)Zakarumiy ~ % python -m timeit "import test; test.prttn2(25,10000)"
10 loops, best of 3: 157 msec per loop

necoro(a)Zakarumiy ~ % python -m timeit "import test; test.prttn3(25,10000)"
10 loops, best of 3: 137 msec per loop

Note: This is probably not representative ... just a quick check to get
a raw feeling.

- René