Python - Consecutive Ranges of K greater than N

When you need to find consecutive ranges of a specific value K that appear at least N times in a row, you can use enumerate() to track positions and identify these ranges.

Problem Understanding

Given a list, find all consecutive sequences where:

  • The value equals K

  • The sequence length is at least N

  • Return the start and end indices of each valid range

Example

Below is a demonstration of finding consecutive ranges ?

my_list = [3, 65, 33, 23, 65, 65, 65, 65, 65, 65, 65, 3, 65]
print("The list is:")
print(my_list)

K = 65  # Value to search for
N = 3   # Minimum consecutive occurrences
print("The value of K is", K)
print("The value of N is", N)

my_result = []
beg, end = 0, 0
previous = None

for index, element in enumerate(my_list):
    if element == K:
        end = index
        # Start new range if previous element wasn't K
        if previous != K:
            beg = index
    else:
        # End of K sequence - check if it's long enough
        if previous == K and end - beg + 1 >= N:
            my_result.append((beg, end))
    
    previous = element

# Check final sequence if list ends with K
if previous == K and end - beg + 1 >= N:
    my_result.append((beg, end))

print("The result is:")
print(my_result)
The list is:
[3, 65, 33, 23, 65, 65, 65, 65, 65, 65, 65, 3, 65]
The value of K is 65
The value of N is 3
The result is:
[(4, 10)]

How It Works

The algorithm tracks consecutive sequences by:

  • Beginning tracking: When finding K after a non-K element, mark the start position

  • End tracking: Update end position for each K encountered

  • Range validation: When a K sequence ends, check if length ? N

  • Result storage: Store valid ranges as (start_index, end_index) tuples

Step-by-Step Breakdown

Let's trace through the example list [3, 65, 33, 23, 65, 65, 65, 65, 65, 65, 65, 3, 65] ?

# Tracing the algorithm
data = [3, 65, 33, 23, 65, 65, 65, 65, 65, 65, 65, 3, 65]
K = 65
N = 3

print("Index | Value | Action")
print("-" * 25)

for i, val in enumerate(data):
    if val == K:
        print(f"{i:5} | {val:5} | Found K - part of sequence")
    else:
        print(f"{i:5} | {val:5} | Not K - sequence break")
        
print(f"\nConsecutive 65s from index 4 to 10 = {10-4+1} occurrences")
print(f"Since {10-4+1} >= {N}, range (4,10) is valid")
Index | Value | Action
-------------------------
    0 |     3 | Not K - sequence break
    1 |    65 | Found K - part of sequence
    2 |    33 | Not K - sequence break
    3 |    23 | Not K - sequence break
    4 |    65 | Found K - part of sequence
    5 |    65 | Found K - part of sequence
    6 |    65 | Found K - part of sequence
    7 |    65 | Found K - part of sequence
    8 |    65 | Found K - part of sequence
    9 |    65 | Found K - part of sequence
   10 |    65 | Found K - part of sequence
   11 |     3 | Not K - sequence break
   12 |    65 | Found K - part of sequence

Consecutive 65s from index 4 to 10 = 7 occurrences
Since 7 >= 3, range (4,10) is valid

Conclusion

This algorithm efficiently finds consecutive ranges by tracking sequence boundaries and validating their length. The approach works well for identifying patterns in data where you need minimum consecutive occurrences of specific values.

Updated on: 2026-03-26T13:14:21+05:30

324 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements