Returns Chute Sorter
MediumOnline Assessment
Simulate a library's book-return conveyor that drops each book into a bin by call number, with an overflow tray and staff emptying bins.
Every call number is one or more capital letters followed by a number, such as QA76 or Z5. Call numbers are ordered by their letters first (alphabetically, so Q comes before QA), and books with identical letters by their number as an integer (QA9 comes before QA76).
The conveyor has bins numbered from 0. Bin i takes every call number from bins[i][0] to bins[i][1], both ends included. Ranges never overlap. Each bin holds at most capacity books, and the overflow tray holds at most overflow_capacity. Events are processed in order:
- A book (the event is its call number) goes to the end of its bin. If the bin is full it goes to the end of the overflow tray instead; if that is full too, it goes to the reject tray. A book that fits no bin's range goes straight to the reject tray.
"empty k"means staff carry away every book in bink(add them to the cart list in the order they were in the bin). Then the overflow tray is scanned from its front; each book that belongs to binkmoves to the end of bink, as long as the bin still has room. The other overflow books stay in place, in order.
Input: bins, a list of [low, high] call-number pairs; capacity and overflow_capacity, integers; events, a list of strings.
Output: a list with one list of call numbers per bin (in bin order, oldest first), followed by the overflow tray, the reject tray, and the cart list: len(bins) + 3 lists in total.
Example 1
- Input:
- [[["A1","M99"],["N1","Z99"]],2,1,["B7","C3","B9","P4","B1","empty 0","A2"]]
- Output:
- [["B9","A2"],["P4"],[],["B1"],["B7","C3"]]
- Explanation
- Bin 0 takes A1 to M99 and bin 1 takes N1 to Z99, each holding 2, with room for 1 book in overflow. B7 and C3 fill bin 0; B9 goes to overflow; P4 goes to bin 1; B1 finds bin 0 and the overflow full and is rejected. Emptying bin 0 carries away B7 and C3, then the overflow's B9 moves into bin 0; A2 arrives afterwards and joins it.
Example 2
- Input:
- [[["QA9","QA76"],["QB1","QB9"]],2,1,["QA10","QA9","QA76","QA77","Q5","QB4"]]
- Output:
- [["QA10","QA9"],["QB4"],["QA76"],["QA77","Q5"],[]]
- Explanation
- QA10 falls in the QA9 to QA76 range because numbers compare as integers (10 is between 9 and 76). QA10 and QA9 fill bin 0, QA76 overflows. QA77 is above the end of bin 0 and below QB1, so it fits no bin and is rejected; Q5 sorts before QA9 (Q comes before QA), so it is rejected too. QB4 goes to bin 1.
Example 3
- Input:
- [[["A1","A9"],["B1","B9"]],1,2,["A1","A2","B1","A3","B2","A4","B3","empty 0","empty 1"]]
- Output:
- [["A2"],[],["A3"],["B2","A4","B3"],["A1","B1"]]
- Explanation
- With capacity 1 and overflow 2: A1 and B1 take their bins, A2 and A3 fill the overflow, so B2, A4 and B3 are all rejected. Emptying bin 0 carries away A1, then the first overflow book belonging to bin 0 (A2) moves in; A3 stays in the overflow. Emptying bin 1 carries away B1; no overflow book belongs to bin 1, so it stays empty.
Constraints
- 1 ≤ bins.length ≤ 8; 1 ≤ capacity ≤ 10; 0 ≤ overflow_capacity ≤ 10
- 0 ≤ events.length ≤ 200
- call numbers are 1 to 3 capital letters followed by an integer from 1 to 9999; bin ranges are valid (low ≤ high) and do not overlap
- empty events name an existing bin