mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-04-20 20:57:35 +00:00
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
parent
f992b5eac6
commit
103d7c48cb
@ -44,23 +44,23 @@ if __name__ == "__main__":
|
|||||||
doctest.testmod()
|
doctest.testmod()
|
||||||
|
|
||||||
|
|
||||||
#counting number of 0s and 1s in a binary number
|
# counting number of 0s and 1s in a binary number
|
||||||
def count_zeros_and_ones(binary_number):
|
def count_zeros_and_ones(binary_number):
|
||||||
# Convert the binary number to a string if it's not already
|
# Convert the binary number to a string if it's not already
|
||||||
binary_str = str(binary_number)
|
binary_str = str(binary_number)
|
||||||
|
|
||||||
count_zeros = binary_str.count('0')
|
count_zeros = binary_str.count("0")
|
||||||
count_ones = binary_str.count('1')
|
count_ones = binary_str.count("1")
|
||||||
|
|
||||||
return count_zeros, count_ones
|
return count_zeros, count_ones
|
||||||
|
|
||||||
|
|
||||||
# Get user input
|
# Get user input
|
||||||
binary_number = input("Enter a binary number: ")
|
binary_number = input("Enter a binary number: ")
|
||||||
|
|
||||||
# Validate input
|
# Validate input
|
||||||
if all(bit in '01' for bit in binary_number):
|
if all(bit in "01" for bit in binary_number):
|
||||||
zeros, ones = count_zeros_and_ones(binary_number)
|
zeros, ones = count_zeros_and_ones(binary_number)
|
||||||
print(f"Number of 0s: {zeros}, Number of 1s: {ones}")
|
print(f"Number of 0s: {zeros}, Number of 1s: {ones}")
|
||||||
else:
|
else:
|
||||||
print("Invalid input! Please enter a valid binary number.")
|
print("Invalid input! Please enter a valid binary number.")
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user