Drawer grids: Boss receipt
Inputs
drawers is a list of rows. Each row is a list of numbers and may be empty.
Your tasks
- Create a list
rowTotals.drawersis a list of rows. Add the numbers in each row. Put one total per row inrowTotals, in row order. An empty row has a total of 0. - Multiply each number in
rowTotalsby 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
drawerGridsBossReceiptso it returnsweightedTotal. Return it, do not print it.
Example
If drawers is [[0, -2], [], [5, 3], [-2, -4], [4, 2]], then rowTotals is [-2, 0, 8, -6, 6] and drawerGridsBossReceipt returns 28.
Rules
- If
rowTotalsis empty, return 0.
Test data
TEST DATA 1: drawers is [[-9, -11], [], [0, -2], [20, 18], [10, 8]].
TEST DATA 2: drawers is [[-4, -6], [], [5, 3], [5, 3]].
Examples
Input: drawers = [[-9,-11],[],[0,-2],[20,18],[10,8]] Output: 216
rowTotals is [-20, 0, -2, 38, 18]. Multiply by positions that start at 1: 1 * (-20) + 2 * (0) + 3 * (-2) + 4 * (38) + 5 * (18) = 216. Return 216.
Input: drawers = [[-4,-6],[],[5,3],[5,3]] Output: 46
rowTotals is [-10, 0, 8, 8]. Multiply by positions that start at 1: 1 * (-10) + 2 * (0) + 3 * (8) + 4 * (8) = 46. Return 46.
Input: drawers = [] Output: 0
rowTotals is []. There are no numbers to multiply or add, so return 0.
Input limits
- 0 <= input size <= 16 per row.
- Answers fit in a signed 32-bit integer: -2,147,483,648 through 2,147,483,647.
- Input numbers have no decimal part. They range from -1,000,000 to 1,000,000. Follow any extra input rules in task 1.
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 rowTotals 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.