Bookcase With Gaps
Implement a class Bookcase. It has endless shelves numbered 0, 1, 2, ... from the top, and each shelf holds at most capacity books, standing in a row. Books are numbers. Write these methods:
Bookcase(capacity)starts with every shelf empty.add(book)puts the book at the end of the row on the lowest-numbered shelf that still has room, and returns the number of that shelf.take()removes the book at the end of the row on the highest-numbered shelf that holds any books, and returns it. With no books at all it returns-1.take_from(shelf)removes the book at the end of the row on that shelf and returns it, or returns-1if that shelf is empty. This can leave a gap that a lateraddfills.count_books()returns the number of books in the bookcase.
The tests call the methods in sequence and compare every return value. The expected list holds null for the constructor.
Example 1
Bookcase(2)
add(10) returns 0
add(20) returns 0
add(30) returns 1
take_from(0) returns 20
add(40) returns 0
take() returns 30
count_books() returns 2
With room for two per shelf, the books 10 and 20 fill shelf 0 and 30 goes to shelf 1. Taking from shelf 0 removes 20 and leaves a gap there, which the next book, 40, fills. The last book on the highest used shelf is still 30.
Constraints
1 <= capacity <= 41 <= book <= 400and0 <= shelf <= 30- Each test makes
1to40method calls after the constructor.
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.