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
Selected Reading
Python program to check if there are K consecutive 1's in a binary number?
Checking for K consecutive 1's in a binary number is a common string pattern matching problem. We can solve this by creating a pattern string of K ones and checking if it exists in the binary number.
Algorithm
The approach involves these steps ?
- Take a binary string input containing only 1's and 0's
- Get the value K (number of consecutive 1's to find)
- Create a pattern string with K consecutive 1's
- Check if this pattern exists in the binary string
Using String Pattern Matching
The simplest approach is to create a pattern string and use the in operator ?
def check_consecutive_ones(binary_str, k):
# Create pattern of k consecutive 1's
pattern = "1" * k
# Check if pattern exists in binary string
if pattern in binary_str:
return True
else:
return False
# Example usage
binary_number = "1111001111"
k = 3
result = check_consecutive_ones(binary_number, k)
if result:
print(f"Found {k} consecutive 1's in {binary_number}")
else:
print(f"No {k} consecutive 1's found in {binary_number}")
Found 3 consecutive 1's in 1111001111
Interactive Version
Here's a version that takes user input ?
def find_consecutive_ones(binary_str, k):
pattern = "1" * k
if pattern in binary_str:
print("Consecutive 1's is Found")
else:
print("Consecutive 1's is Not Found")
# Test with sample data
binary_str = "1111001111"
k = 3
print(f"Binary number: {binary_str}")
print(f"Looking for {k} consecutive 1's:")
find_consecutive_ones(binary_str, k)
# Test with different values
print("\nAnother test:")
binary_str2 = "1010101"
k2 = 2
print(f"Binary number: {binary_str2}")
print(f"Looking for {k2} consecutive 1's:")
find_consecutive_ones(binary_str2, k2)
Binary number: 1111001111 Looking for 3 consecutive 1's: Consecutive 1's is Found Another test: Binary number: 1010101 Looking for 2 consecutive 1's: Consecutive 1's is Not Found
Using Regular Expressions
For more complex pattern matching, we can use regular expressions ?
import re
def check_consecutive_ones_regex(binary_str, k):
# Create pattern for k consecutive 1's
pattern = f"1{{{k}}}"
# Search for the pattern
match = re.search(pattern, binary_str)
return match is not None
# Test the function
binary_number = "1111001111"
k = 4
if check_consecutive_ones_regex(binary_number, k):
print(f"Found {k} consecutive 1's")
else:
print(f"No {k} consecutive 1's found")
# Find all occurrences
matches = re.findall(f"1{{{k}}}", binary_number)
print(f"Number of occurrences: {len(matches)}")
Found 4 consecutive 1's Number of occurrences: 1
Comparison
| Method | Time Complexity | Best For |
|---|---|---|
String in operator |
O(n) | Simple pattern matching |
| Regular expressions | O(n) | Complex patterns, finding all matches |
| Manual iteration | O(n) | Custom logic, counting matches |
Conclusion
The string pattern matching approach using "1" * k in binary_str is the simplest and most efficient method. Use regular expressions when you need to find all occurrences or handle more complex patterns.
Advertisements
