From f992b5eac63521fd7ff1327fe2c4d887030da533 Mon Sep 17 00:00:00 2001 From: USP-2024 Date: Sun, 13 Oct 2024 19:43:04 +0530 Subject: [PATCH 1/4] Update binary_count_trailing_zeros.py The program prompts the user to enter a binary number. It checks if the input contains only 0s and 1s using a generator expression. If the input is valid, it calls the count_zeros_and_ones function and prints the results. If not, it notifies the user of the invalid input. --- .../binary_count_trailing_zeros.py | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/bit_manipulation/binary_count_trailing_zeros.py b/bit_manipulation/binary_count_trailing_zeros.py index f401c4ab9266..702672e0719c 100644 --- a/bit_manipulation/binary_count_trailing_zeros.py +++ b/bit_manipulation/binary_count_trailing_zeros.py @@ -42,3 +42,25 @@ def binary_count_trailing_zeros(a: int) -> int: import doctest doctest.testmod() + + +#counting number of 0s and 1s in a binary number +def count_zeros_and_ones(binary_number): + # Convert the binary number to a string if it's not already + binary_str = str(binary_number) + + count_zeros = binary_str.count('0') + count_ones = binary_str.count('1') + + return count_zeros, count_ones + +# Get user input +binary_number = input("Enter a binary number: ") + +# Validate input +if all(bit in '01' for bit in binary_number): + zeros, ones = count_zeros_and_ones(binary_number) + print(f"Number of 0s: {zeros}, Number of 1s: {ones}") +else: + print("Invalid input! Please enter a valid binary number.") + From 103d7c48cba97fcc29366d58227ef0748c98f4a8 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 13 Oct 2024 14:16:50 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- bit_manipulation/binary_count_trailing_zeros.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/bit_manipulation/binary_count_trailing_zeros.py b/bit_manipulation/binary_count_trailing_zeros.py index 702672e0719c..cdf530f1f4b1 100644 --- a/bit_manipulation/binary_count_trailing_zeros.py +++ b/bit_manipulation/binary_count_trailing_zeros.py @@ -44,23 +44,23 @@ def binary_count_trailing_zeros(a: int) -> int: 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): # Convert the binary number to a string if it's not already binary_str = str(binary_number) - - count_zeros = binary_str.count('0') - count_ones = binary_str.count('1') - + + count_zeros = binary_str.count("0") + count_ones = binary_str.count("1") + return count_zeros, count_ones + # Get user input binary_number = input("Enter a binary number: ") # 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) print(f"Number of 0s: {zeros}, Number of 1s: {ones}") else: print("Invalid input! Please enter a valid binary number.") - From ab7bcb99bc2a09c069bbad766ae2533d50134d09 Mon Sep 17 00:00:00 2001 From: USP-2024 Date: Sun, 13 Oct 2024 20:07:18 +0530 Subject: [PATCH 3/4] Update binary_count_trailing_zeros.py The function count_zeros_and_ones takes a binary number as input. It converts the number to a string and counts the occurrences of 0 and 1 using the count() method. It returns the counts, which are then printed out. You can replace binary_number with any binary number you'd like to analyze! --- .../binary_count_trailing_zeros.py | 21 +++++++------------ 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/bit_manipulation/binary_count_trailing_zeros.py b/bit_manipulation/binary_count_trailing_zeros.py index cdf530f1f4b1..6cdded395c30 100644 --- a/bit_manipulation/binary_count_trailing_zeros.py +++ b/bit_manipulation/binary_count_trailing_zeros.py @@ -48,19 +48,14 @@ def binary_count_trailing_zeros(a: int) -> int: def count_zeros_and_ones(binary_number): # Convert the binary number to a string if it's not already binary_str = str(binary_number) - - count_zeros = binary_str.count("0") - count_ones = binary_str.count("1") - + + count_zeros = binary_str.count('0') + count_ones = binary_str.count('1') + return count_zeros, count_ones +# Example usage +binary_number = 1011001 +zeros, ones = count_zeros_and_ones(binary_number) +print(f"Number of 0s: {zeros}, Number of 1s: {ones}") -# Get user input -binary_number = input("Enter a binary number: ") - -# Validate input -if all(bit in "01" for bit in binary_number): - zeros, ones = count_zeros_and_ones(binary_number) - print(f"Number of 0s: {zeros}, Number of 1s: {ones}") -else: - print("Invalid input! Please enter a valid binary number.") From 5d49809fd599a909d1f7ee4d0ae0fb35762bd1a1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 13 Oct 2024 14:37:39 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- bit_manipulation/binary_count_trailing_zeros.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/bit_manipulation/binary_count_trailing_zeros.py b/bit_manipulation/binary_count_trailing_zeros.py index 6cdded395c30..1168521d4f3d 100644 --- a/bit_manipulation/binary_count_trailing_zeros.py +++ b/bit_manipulation/binary_count_trailing_zeros.py @@ -48,14 +48,14 @@ def binary_count_trailing_zeros(a: int) -> int: def count_zeros_and_ones(binary_number): # Convert the binary number to a string if it's not already binary_str = str(binary_number) - - count_zeros = binary_str.count('0') - count_ones = binary_str.count('1') - + + count_zeros = binary_str.count("0") + count_ones = binary_str.count("1") + return count_zeros, count_ones + # Example usage binary_number = 1011001 zeros, ones = count_zeros_and_ones(binary_number) print(f"Number of 0s: {zeros}, Number of 1s: {ones}") -