Your question is Class Tracker With Nearest5. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
Khan Academy may need to inspect the five learning-event values closest to a target while new values continue arriving. Implement the behavior of a Tracker that supports efficient insertion and nearest-value queries.
For this coding exercise, expose the behavior through one function, tracker_operations(operations), which internally maintains the tracker. Each operation is an object with op, either add or nearest5, and an integer value.
add(value) inserts one occurrence of value; duplicate values must be preserved.nearest5(value) returns up to five stored numbers ordered by increasing absolute distance from value.nearest5 operation, in query order.Input: operations, a list of operation objects with string field op and integer field value.
Output: A list of integer lists. Each inner list contains at most five stored values for one nearest5 operation.
def tracker_operations(operations):