Ribbon runs: Boss receipt
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]. - Multiply each number in
runLengthsby its position, counting from 1: the first number by 1, the second by 2, and so on. - Add those products. Store the sum in
weightedTotal. - Complete
ribbonRunsBossReceiptso it returnsweightedTotal. Return it, do not print it.
Example
If line is "jp pvb", then runLengths is [1, 1, 2, 1, 1, 1] and ribbonRunsBossReceipt returns 24.
Rules
- If
runLengthsis empty, return 0.
Test data
TEST DATA 1: line is "kuuow bnnnq".
TEST DATA 2: line is "UU uUU".
Examples
Input: line = "kuuow bnnnq" Output: 52
runLengths is [1, 2, 1, 1, 1, 1, 3, 1]. Multiply by positions that start at 1: 1 * (1) + 2 * (2) + 3 * (1) + 4 * (1) + 5 * (1) + 6 * (1) + 7 * (3) + 8 * (1) = 52. Return 52.
Input: line = "UU uUU" Output: 15
runLengths is [2, 1, 1, 2]. Multiply by positions that start at 1: 1 * (2) + 2 * (1) + 3 * (1) + 4 * (2) = 15. Return 15.
Input: line = "" Output: 0
runLengths is []. There are no numbers to multiply or add, so return 0.
Input limits
- 0 <= input size <= 64.
- 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.