Skip to content

Instantly share code, notes, and snippets.

@rkalit
Created November 5, 2019 16:00
Show Gist options
  • Select an option

  • Save rkalit/c55b578b97878fbe497dd932217348b0 to your computer and use it in GitHub Desktop.

Select an option

Save rkalit/c55b578b97878fbe497dd932217348b0 to your computer and use it in GitHub Desktop.
#!usr/bin/python
def countBits(n, carry):
binNum = []
car0 = 0
car1 = 0
def decToBin(n):
while(n>0):
if n % 2 == 0:
binNum.append(0)
n = n/2
else:
binNum.append(1)
n = n//2
decToBin(n)
for x in range(len(binNum)):
if x == 0:
car0 += 1
else:
car1 += 1
if carry == 0:
print(f'Total 0: {car0}')
elif carry == 1:
print(f'Total 1: {car1}')
else:
print('null')
def main():
countBits(13,0) # Test Case 1
countBits(13,1) # test Case 2
countBits(13,2) # Test Case 3
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment