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
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
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
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
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:
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)
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, 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,
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
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
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
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
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
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
14 matches
Mail list logo