Created
November 5, 2019 16:00
-
-
Save rkalit/c55b578b97878fbe497dd932217348b0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| #!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