You are given a collection of boxes, each with its own size. You are then given products one at a time, and you have to choose a large enough box for each product. Each box can be used only once.
Example: The box sizes are and you are given products as follows:
- The size of the product is . You choose the box of size .
- The size of the product is . You choose the box of size .
- The size of the product is . You choose a box of size .
- The size of the product is . There is no box large enough.
- The size of the product is . You choose the box of size .
In a file nextbox.py
, implement the function find_boxes
that takes a list of box sizes and a list of products sizes in order of processing as parameters. The function should return a list of the box sizes chosen for each products. The box size should be if no box was found for the product.
The function should be efficient even if both lists are long. The last function call in the code template tests this situation.
def find_boxes(boxes, products): # TODO if __name__ == "__main__": print(find_boxes([4, 4, 6, 8], [5, 5, 4, 6, 1])) # [6, 8, 4, -1, 4] print(find_boxes([1, 2, 3, 4], [1, 1, 1, 1, 1])) # [1, 2, 3, 4, -1] print(find_boxes([2, 2, 2, 2], [1, 1, 1, 1, 1, 1])) # [2, 2, 2, 2, -1, -1] print(find_boxes([1, 1, 1, 1], [2, 2])) # [-1, -1] boxes = [] products = [] for i in range(10**5): boxes.append(i % 100 + 1) products.append(3 * i % 97 + 1) result = find_boxes(boxes, products) print(result[42]) # 30 print(result[1337]) # 35 print(result[-1]) # 100