jak writes:
[...]
>> --8<---cut here---start->8---
>> def powers_of_2_in(n):
>>if remainder(n, 2) != 0:
>> return 0, n
>>else:
>> s, r = powers_of_2_in(n // 2)
>> return 1 + s, r
>> --8<---cut here---end--
Alan Bawden writes:
> jak writes:
>
>Alan Bawden ha scritto:
>> Julieta Shem writes:
>>
>> How would you write this procedure?
>> def powers_of_2_in(n):
>> ...
>>
>> def powers_of_2_in(n):
>> return (n ^ (n - 1)).bit_count() - 1
>>
>
How would you write this procedure?
--8<---cut here---start->8---
def powers_of_2_in(n):
s = 0
while "I still find factors of 2 in n...":
q, r = divmod(n, 2)
if r == 0:
s = s + 1
n = n // 2
else:
return s, n
--8<---c
Julieta Shem writes:
[...]
> I agree. By the way, I once read or watched an interview with Guido van
> Rossum and and he was asked why not to tail-call optimize Python and the
> answer he gave --- IIRC --- was that tail-call optimization makes it
> harder for a beginner to understand a stack tr
Greg Ewing writes:
> On 8/11/23 2:26 pm, Julieta Shem wrote:
>> For the first time I'm trying to write a tail-recursive
>> square-and-multiply and, even though it /seems/ to work, I'm not happy
>> with what I wrote and I don't seem to understand it so well.
>
> Stepping back a bit, why do you fee
For the first time I'm trying to write a tail-recursive
square-and-multiply and, even though it /seems/ to work, I'm not happy
with what I wrote and I don't seem to understand it so well.
--8<---cut here---start->8---
def sam(b, e, m, acc = 1):
if e == 0: