Ribbon runs: First qualifying slip
Inputs
line is text. A character is one letter, digit, space, or punctuation mark.
Your tasks
- Create a list
runLengths. A run is a group of matching characters next to each other. End a run only when the character changes or the text ends. Record the length of each run, from left to right. Spaces count as characters too. For "aa bb",runLengthsis [2, 1, 2]. - Read
runLengthsfrom left to right. Find the first number that is at leastkand store that number, not its position, infirstMatch. - Complete
ribbonRunsFirstQualifyingSlipso it returnsfirstMatch. Return it, do not print it.
Example
If line is "zu gid" and k is 1, then runLengths is [1, 1, 2, 1, 1, 1] and ribbonRunsFirstQualifyingSlip returns 1.
Rules
- If no number is at least
k, return -1. k(a whole number from 1 to 8) is the threshold in task 2: compare each number with it.
Test data
TEST DATA 1: line is "niizx xvvvi" and k is 4.
TEST DATA 2: line is "II iII" and k is 1.
Examples
Input: line = "niizx xvvvi" k = 4 Output: -1
runLengths is [1, 2, 1, 1, 1, 1, 3, 1]. Here k is 4. No number is at least 4, so return -1.
Input: line = "II iII" k = 1 Output: 2
runLengths is [2, 1, 1, 2]. Here k is 1. The first number at least 1 is 2. Return that value.
Input: line = "" k = 7 Output: -1
runLengths is []. Here k is 7. No number is at least 7, so return -1.
Input limits
- 0 <= input size <= 64.
- k is a whole number from 1 to 8. Answers fit in a signed 32-bit integer: -2,147,483,648 through 2,147,483,647.
- The text uses ASCII characters (a basic character set). Only an ordinary space separates words.
Efficiency goal
Big-O describes how the work or memory needed grows as the input gets larger.
Efficiency goal: time O(n); extra memory, including the helper list, O(n). n is the input length. These labels describe how work and memory grow with the input.
Practice focus
Build runLengths first, then return exactly what the tasks ask for. Follow the order, keep repeated values when the rule says so, and check the empty case.