Counting runs
A run is a stretch of neighbouring items that share a property: equal letters, days over a goal, ones in a row.
The standard pattern keeps a running length that grows while the run goes on and resets when it ends:
run = 0 for x in items: if x is part of the run: run = run + 1 else: run = 0
Then decide what you report: the longest run (keep a best updated every pass, not only when a run ends, or a run at the very end of the list is lost), how many runs there are, or runs of at least some size.
Two habits:
- Comparing with the previous item (
word[i] == word[i - 1]) needsi >= 1. - To count each run once, count it at the moment it starts or reaches its size, never at every item.
Watch run climb and fall, and best remember the peak.
See it run
word = "aabbba" run = 1 best = 1 for i in range(1, len(word)): if word[i] == word[i - 1]: run = run + 1 else: run = 1 best = max(best, run)
The loop compares each letter with the one before it, starting at position 1.
Practice counting runs
Exercise 1
A shop records its daily sales as whole numbers. A day is good when its sales are at least goal. Return the length of the longest streak of consecutive good days, as an integer.
Rules:
- Sales equal to
goalcount as good. - If there is no good day (or no days), return
0.
Constraints: 0 ≤ len(sales) ≤ 10^5, sales and goal between -10^6 and 10^6.
Example: solution([5, 9, 9, 2, 9, 9, 9], 8) returns 3 (the last three days).
Exercise 2
A message got stuck on a repeating key. Clean it: in text, no character may appear more than twice in a row. Whenever a run of the same character is longer than 2, keep only the first 2 characters of it.
Return the cleaned string.
Rules:
- A run is made of identical neighbouring characters (letters, digits, spaces; case matters).
- The same character may appear again later, after a different one, and then starts a new run.
Constraints: 0 ≤ len(text) ≤ 10^5.
Example: solution("aaaabaaa") returns "aabaa".
Exercise 3
A run is a stretch of identical neighbouring characters. Write solution(text, k) returning how many runs in text have length at least k.
Rules:
- Each run counts once, however long it is:
"aaaa"is a single run of length 4. - The same character in two separate places is two different runs.
kis at least 1. Empty text gives0.
Constraints: 0 ≤ len(text) ≤ 10^5, 1 ≤ k ≤ 10^5.
Example: solution("aaabbcccc", 3) returns 2 (the a's and the c's; the b's are too short).
Exercise 4
Count the stretches of equal values. A flat segment is a block of one or more neighbouring positions of nums whose values are all equal. Different positions make different segments, even when they hold the same numbers.
Return how many flat segments nums contains, as an integer.
Rules:
- A single position is a flat segment.
- An empty list has
0segments.
Constraints: 0 ≤ len(nums) ≤ 2·10^4. Values are whole numbers between -10^9 and 10^9. Long runs of equal values are possible, so the code must be fast.
Example: solution([1, 1, 2]) returns 4: [1] (first), [1] (second), [1, 1] and [2].
The explanation and tasks are public. Running this lesson requires an account with course access.
Practice
Loading Python…Visualizer Watch your code run line by line
Run your code, then choose Play to follow its execution.