Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Binary Count by checinski.szymon
def biggestSecondPower(number):
result = 1
while result < number:
result *= 2
#print (str(result) + " " + str(number))
if result == number:
return result
else:
return result // 2
def checkio(number):
unities = 0
power = biggestSecondPower(number)
#print (power)
if power == number:
return 1
while number > 0:
number -= power
unities += 1
power = biggestSecondPower(number)
return unities
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert checkio(4) == 1
assert checkio(15) == 4
assert checkio(1) == 1
assert checkio(1022) == 9
Nov. 6, 2016