python - count number of ones in a given integer -
How do you calculate the number of people in the binary representation of the given integer.
Say you have given a number, Use the awesome module. Or you can use the built-in function Or even: But this final solution is slower than 20 , which is in binary,
10100 , so many people have 2 numbers.
& gt; & Gt; & Gt; Counter import from & gt; & Gt; & Gt; Binary = Bin (20) [2:]> gt; & Gt; & Gt; Counter (binary) counter ({'0': 3, '1': 2})
count () :
& gt; & Gt; & Gt; Binary = Bin (20) [2:]> gt; & Gt; & Gt; BinaryCount ('1') 2
& gt; & Gt; & Gt; (Amount for bin (20) in (I): If I == '1' 2)
count (< / Code>
Comments
Post a Comment