在随机生成的数组中查找连续非递减整数序列的编程需求
Alright, let's break down how to solve this problem—generating the random array and finding those consecutive non-decreasing sequences—with straightforward, step-by-step code and explanations.
Step 1: Generate the Random Integer Array
First, we need to create our array of 200 random integers between 25 and 225. In Python, this is super simple using the built-in random module:
import random # Generate array of 200 random integers (25 to 225 inclusive) random_array = [random.randint(25, 225) for _ in range(200)]
A quick breakdown here:
random.randint(25, 225)ensures we get integers spanning exactly the range you specified (it includes both endpoints).- The list comprehension efficiently creates 200 elements in one line—no need for messy loops just to populate the array.
Step 2: Find Consecutive Non-Decreasing Sequences of Length n
Next, we'll build a function to scan the array and pull out all sequences of n consecutive non-decreasing numbers. The core logic is to track the length of the current non-decreasing streak, and whenever that streak meets or exceeds n, we extract all valid sub-sequences from it.
Here's the code:
def find_consecutive_non_decreasing(arr, n): # Edge case: Any single element is trivially non-decreasing if n <= 1: return arr sequences = [] current_streak_length = 1 streak_start_index = 0 for i in range(1, len(arr)): # Extend the streak if current element is >= previous if arr[i] >= arr[i-1]: current_streak_length += 1 else: # Check if the previous streak was long enough for our target n if current_streak_length >= n: # Extract all possible n-length sequences from this streak for j in range(streak_start_index, streak_start_index + current_streak_length - n + 1): sequences.append(arr[j:j+n]) # Reset tracker for the new streak current_streak_length = 1 streak_start_index = i # Don't forget to check the final streak after the loop ends! if current_streak_length >= n: for j in range(streak_start_index, streak_start_index + current_streak_length - n + 1): sequences.append(arr[j:j+n]) return sequences # Example usage: Find all sequences of length 5 target_length = 5 found_sequences = find_consecutive_non_decreasing(random_array, target_length) # Print results print(f"Found {len(found_sequences)} valid sequences of length {target_length}:") for seq in found_sequences: print(seq)
Key Details About the Function:
- Edge Case Handling: If
nis 1 or smaller, every element counts as a valid sequence, so we just return the whole array. - Streak Tracking: We keep track of how long the current non-decreasing run is, and where it starts. When we hit a number that breaks the streak, we check if the previous run was long enough to have valid
n-length sequences. - Avoiding Misses: After the loop finishes, we have to check the final streak—since the array might end with a valid sequence that never got processed.
- All Valid Sequences: For longer streaks (e.g., a run of 6 non-decreasing numbers), we extract every possible
n-length sub-sequence (so 2 sequences of length 5 from a 6-length streak).
Optional Tweaks:
- If you only need the first valid sequence (not all of them), you can modify the function to return immediately when it finds the first one—this saves computation time.
- If you want the longest non-decreasing sequence instead of fixed-length sequences, adjust the logic to track the maximum streak length and its corresponding subarray.
内容的提问来源于stack exchange,提问作者Alex S
相关产品推荐
相关产品推荐

