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 duncan.bo...@invalid.invalid writes: Mark Dickinson dicki...@gmail.com 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

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 c...@rebertia.com 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

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 duncan.bo...@invalid.invalid: Mark Dickinson dicki...@gmail.com 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

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 duncan.bo...@invalid.invalid: 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

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 digi...@gmail.com wrote: 2009/2/5 Duncan Booth duncan.bo...@invalid.invalid: Mark Dickinson dicki...@gmail.com 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:

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 dicki...@gmail.com 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)

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 c...@rebertia.com 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') %

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,

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 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

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 binary has an odd or even

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 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

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
A byte is *not* a Python type. My question was what *Python type* (i.e. bytes (which is distinctly different from the abstract notion of a byte), str/unicode, int, etc...) you were using for you binary representation, which you still haven't answered. Also, please don't reply by top-posting

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 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