Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Check if the given decimal number has 0 and 1 digits only in Python
Sometimes we need to check if a decimal number contains only binary digits (0 and 1). This is useful for validating binary representations or checking if a number can be interpreted as a binary string.
So, if the input is like num = 101101, then the output will be True since it contains only 0s and 1s.
Algorithm
To solve this, we will follow these steps −
- Extract all digits from the number into a set
- Remove 0 and 1 from the digits set
- If the set is empty after removal, return True
- Otherwise, return False
Using Set Operations
We can extract digits, store them in a set, and check if only 0 and 1 remain ?
def solve(num):
digits_set = set()
while num > 0:
digit = num % 10
digits_set.add(digit)
num = int(num / 10)
digits_set.discard(0)
digits_set.discard(1)
if len(digits_set) == 0:
return True
return False
# Test the function
num = 101101
print(f"Number: {num}")
print(f"Contains only 0s and 1s: {solve(num)}")
# Test with other numbers
test_cases = [101101, 1010, 123, 1111, 10203]
for test_num in test_cases:
print(f"{test_num}: {solve(test_num)}")
Number: 101101 Contains only 0s and 1s: True 101101: True 1010: True 123: False 1111: True 10203: False
Using String Conversion Method
A simpler approach is to convert the number to string and check each character ?
def check_binary_digits(num):
num_str = str(num)
for char in num_str:
if char not in ['0', '1']:
return False
return True
# Test the function
test_numbers = [101101, 1010, 123, 1111, 10203, 0, 1]
for num in test_numbers:
result = check_binary_digits(num)
print(f"{num}: {result}")
101101: True 1010: True 123: False 1111: True 10203: False 0: True 1: True
Using Set Comparison
We can also use set operations to check if all digits are subset of {0, 1} ?
def is_binary_number(num):
digits = set(str(num))
binary_digits = {'0', '1'}
return digits.issubset(binary_digits)
# Test the function
numbers = [101101, 1010, 123, 1111, 10203]
for num in numbers:
result = is_binary_number(num)
print(f"{num} contains only 0s and 1s: {result}")
101101 contains only 0s and 1s: True 1010 contains only 0s and 1s: True 123 contains only 0s and 1s: False 1111 contains only 0s and 1s: True 10203 contains only 0s and 1s: False
Comparison
| Method | Approach | Time Complexity | Best For |
|---|---|---|---|
| Set Operations | Extract digits mathematically | O(d) | Mathematical operations |
| String Conversion | Convert to string and iterate | O(d) | Simple and readable |
| Set Comparison | Use subset comparison | O(d) | Concise code |
Conclusion
All three methods effectively check if a number contains only 0 and 1 digits. The string conversion method is most readable, while set comparison provides the most concise solution. Choose based on your preference for readability versus brevity.
