Duncan Booth writes:
> Mark Dickinson wrote:
[snip]
>> while n:
>> count += 1
>> n &= n-1
>> return count
>>
>> is_even = count_set_bits(the_int) % 2 == 0
>>
>> ...but anyone submitting this as a homework
>> solution had better be prepared to explain why
>> it works.
>>
Mark Dickinson wrote:
On Feb 5, 1:18 am, Chris Rebert wrote:
For an integer:
is_even = bin(the_int)[2:].count('1') % 2 == 0
But the OP has to use if and while. How about:
while 2+2 != 5:
if 'wkw' in 'just being awkward':
is_even = bin(the_int)[2:].count('1') % 2 == 0
bre
2009/2/5 Duncan Booth :
>>> I remember a programming exercise when I was an undergraduate and
>>> anyone who *didn't* use that trick got marked down for writing
>>> inefficient code.
>>
>> Is adding and a modulus *really^ more efficient than flipping a bool
>> as I suggested? I think I'd want to s
Tim Rowe wrote:
> 2009/2/5 Duncan Booth :
>> Mark Dickinson wrote:
>>
>>> def count_set_bits(n):
>>> # make sure we include an if, to
>>> # satisfy OP's requirements:
>>> if n < 0:
>>> raise ValueError
>>> count = 0
>>> while n:
>>> count += 1
>>> n &=
2009/2/5 Duncan Booth :
> Mark Dickinson wrote:
>
>> def count_set_bits(n):
>> # make sure we include an if, to
>> # satisfy OP's requirements:
>> if n < 0:
>> raise ValueError
>> count = 0
>> while n:
>> count += 1
>> n &= n-1
>> return count
>>
>>
Mark Dickinson wrote:
> def count_set_bits(n):
> # make sure we include an if, to
> # satisfy OP's requirements:
> if n < 0:
> raise ValueError
> count = 0
> while n:
> count += 1
> n &= n-1
> return count
>
> is_even = count_set_bits(the_int) % 2
On Feb 5, 1:18 am, Chris Rebert wrote:
> For an integer:
> is_even = bin(the_int)[2:].count('1') % 2 == 0
But the OP has to use if and while. How about:
while 2+2 != 5:
if 'wkw' in 'just being awkward':
is_even = bin(the_int)[2:].count('1') % 2 == 0
break
or (Python 2.5 com
todp...@hotmail.com wrote:
> By "binary representation", I mean a byte of 0s and 1s. Example: 0101
> Also, I'm interested in only using while loop and if statement
> to accomplish this task.
> Thanks.
>
I smell homework. Do it yourself!
--
Steve Holden+1 571 484 6266 +1 800 494 3119
Using while loop and if statement, I'm trying to get Python to
tell me whether there are even or odd number of 1's in a
binary representation. For example, if I give Python a
0111, then I want it to say that the binary representation
given has an odd number of 1's. If I give it 00010111, then
:
> By "binary representation", I mean a byte of 0s and 1s. Example: 0101
> Also, I'm interested in only using while loop and if statement to accomplish
> this task.
> Thanks.
>
>> Date: Wed, 4 Feb 2009 17:18:25 -0800
>> Subject: Re: Using while loop an
2009/2/5 todp...@hotmail.com :
> Using while loop and if statement, I'm trying to get Python to tell me
> whether there are even or odd number of 1's in a binary representation.
> For example, if I give Python a 0111, then I want it to say that the
> binary representation given has an odd numbe
By "binary representation", I mean a byte of 0s and 1s. Example: 0101
Also, I'm interested in only using while loop and if statement to accomplish
this task.
Thanks.> Date: Wed, 4 Feb 2009 17:18:25 -0800> Subject: Re: Using while loop
and if statement to tell if a bina
On Wed, Feb 4, 2009 at 5:02 PM, todp...@hotmail.com wrote:
> Using while loop and if statement, I'm trying to get Python to tell me
> whether there are even or odd number of 1's in a binary representation.
> For example, if I give Python a 0111, then I want it to say that the
> binary represen
Using while loop and if statement, I'm trying to get Python to tell me whether
there are even or odd number of 1's in a binary representation.
For example, if I give Python a 0111, then I want it to say that the binary
representation given has an odd number of 1's.
If I give it 00010111, the
14 matches
Mail list logo