Bit ribbons: Boss receipt
Stuck or curious how this works? Visualize Binary Search ↗
Inputs
readings is a list of integers (numbers with no decimal part). It may be empty, and numbers may repeat unless task 1 says otherwise.
Your tasks
- Create a list
oneCounts. Write each number inreadingsin binary, the number system that uses only 0 and 1. Count the 1s in that form. For example, 5 is 101 in binary, so its count is 2. The count for 0 is 0. Put the counts in input order. - Multiply each number in
oneCountsby 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
bitRibbonsBossReceiptso it returnsweightedTotal. Return it, do not print it.
Example
If readings is [3, 3, 2, 3, 9], then oneCounts is [2, 2, 1, 2, 2] and bitRibbonsBossReceipt returns 27.
Rules
- If
oneCountsis empty, return 0.
Test data
TEST DATA 1: readings is [0, 18, 5, 18, 8].
TEST DATA 2: readings is [8, 8, 8, 8].
Examples
Input: readings = [0,18,5,18,8] Output: 23
oneCounts is [0, 2, 2, 2, 1]. Multiply by positions that start at 1: 1 * (0) + 2 * (2) + 3 * (2) + 4 * (2) + 5 * (1) = 23. Return 23.
Input: readings = [8,8,8,8] Output: 10
oneCounts is [1, 1, 1, 1]. Multiply by positions that start at 1: 1 * (1) + 2 * (1) + 3 * (1) + 4 * (1) = 10. Return 10.
Input: readings = [] Output: 0
oneCounts 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.
- Input numbers are whole numbers from 0 through 1000000.
Efficiency goal
Big-O describes how the work or memory needed grows as the input gets larger.
Efficiency goal: time O(n log M); extra memory, including the helper list, O(n). n is the input length; M is the largest input number. These labels describe how work and memory grow with the input.
Practice focus
Build oneCounts 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.