Re: Using while loop and if statement to tell if a binary has an odd or even number of 1's.

2009-02-06 Thread Mark Wooding
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. >>

Re: Using while loop and if statement to tell if a binary has an odd or even number of 1's.

2009-02-05 Thread MRAB
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

Re: Using while loop and if statement to tell if a binary has an odd or even number of 1's.

2009-02-05 Thread Tim Rowe
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

Re: Using while loop and if statement to tell if a binary has an odd or even number of 1's.

2009-02-05 Thread Duncan Booth
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 &=

Re: Using while loop and if statement to tell if a binary has an odd or even number of 1's.

2009-02-05 Thread Tim Rowe
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 >> >>

Re: Using while loop and if statement to tell if a binary has an odd or even number of 1's.

2009-02-05 Thread 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 > > is_even = count_set_bits(the_int) % 2

Re: Using while loop and if statement to tell if a binary has an odd or even number of 1's.

2009-02-05 Thread Mark Dickinson
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

Re: Using while loop and if statement to tell if a binary has an odd or even number of 1's.

2009-02-04 Thread Steve Holden
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

Re: Using while loop and if statement to tell if a binary has an odd or even number of 1's.

2009-02-04 Thread Tim Chase
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

Re: Using while loop and if statement to tell if a binary has an odd or even number of 1's.

2009-02-04 Thread Chris Rebert
: > 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

Re: Using while loop and if statement to tell if a binary has an odd or even number of 1's.

2009-02-04 Thread Tim Rowe
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

RE: Using while loop and if statement to tell if a binary has an odd or even number of 1's.

2009-02-04 Thread todp...@hotmail.com
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

Re: Using while loop and if statement to tell if a binary has an odd or even number of 1's.

2009-02-04 Thread Chris Rebert
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 to tell if a binary has an odd or even number of 1's.

2009-02-04 Thread 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 number of 1's. If I give it 00010111, the