Host Diversity Pagination
Start Timer
0:00:00
Let’s say we have ranked search results for Airbnb listings, each a (listing_id, host_id) tuple. Write a function paginate that splits them into pages of page_size. Keep listings in ranking order, but no host can take more than max_per_host spots on a page unless there aren’t enough other hosts left to fill it. Listings bumped from a page move on to the next one.
Note: Listings stay in ranking order within a page, and the same input should always give the same pages.
Example:
Input:
listings = [(1, 'a'), (2, 'a'), (3, 'b'), (4, 'a'), (5, 'c'), (6, 'b')]
page_size = 3
max_per_host = 1
Output:
def paginate(listings, page_size, max_per_host) ->
[[(1, 'a'), (3, 'b'), (5, 'c')], [(2, 'a'), (4, 'a'), (6, 'b')]]
.
.
.
.
.
.
.
.
.
Comments