Created
May 6, 2013 03:53
-
-
Save vjames19/5523274 to your computer and use it in GitHub Desktop.
Number of Ones code eval
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import math | |
def numberOfOnes(n): | |
mask = 1 | |
bits = n.bit_length() | |
if n < 0: | |
bits +=1 | |
count = 0 | |
for i in range(0, bits): | |
count += n & mask | |
n = n >> 1 | |
print count | |
test_cases = open(sys.argv[1], 'r') | |
for test in test_cases: | |
if test: | |
numberOfOnes(int(test.strip())) | |
test_cases.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment