Magnitude order: Positive proof strip
Stuck or curious how this works? Visualize Sorting ↗
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
byDistance. Sortreadingsby distance from 0, from nearest to farthest. This distance is called absolute value: both -4 and 4 have distance 4. When two numbers have the same distance, put the smaller number first. Keep all repeated numbers. For [3, -2, 2, -3],byDistanceis [-2, 2, -3, 3]. - Create a list
positiveValuesthat keeps only the numbers inbyDistancethat are greater than 0, in the same order. - Complete
magnitudeOrderPositiveProofStripso it returnspositiveValues. Return it, do not print it.
Example
If readings is [3, 2, 8, -1, 3], then byDistance is [-1, 2, 3, 3, 8] and magnitudeOrderPositiveProofStrip returns [2, 3, 3, 8].
Rules
- Keep repeated numbers.
- If no number is greater than 0, return [].
Test data
TEST DATA 1: readings is [2, 0, -8, 15, 15].
TEST DATA 2: readings is [3, -5, 3, 3].
Examples
Input: readings = [2,0,-8,15,15] Output: [2,15,15]
byDistance is [0, 2, -8, 15, 15]. Keep only numbers greater than 0, without changing their order. Return [2, 15, 15].
Input: readings = [3,-5,3,3] Output: [3,3,3]
byDistance is [3, 3, 3, -5]. Keep only numbers greater than 0, without changing their order. Return [3, 3, 3].
Input: readings = [] Output: []
byDistance is []. Keep only numbers greater than 0, without changing their order. Return [].
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 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 log 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 byDistance 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.