Packing Melons
Before you are two assembly lines, one with boxes and one with watermelons, both of varying size. Your desire is to put as many watermelons into boxes as possible. You can pick where you
start taking watermelons from, but once you start, all melons going past you must be placed, or you must stop. We want you to calculate how many watermelons you can place.
As input, you are given two lists, one of box sizes and one of watermelon sizes. A watermelon will fit into a box with a number greater than or equal to the melon’s. Only one melon can go
into a box. You can hold onto the melon and skip a box to place it in a later one. Calculate how many watermelons can be placed.
- Output Format
A list of labels.
- Sample Input
trades = [[99.0, 5.0, 20.0], # green (good trade)
[95.0, 15.0, 10.0], # green (good trade)
[5.0, 80.0, 40.0], # red (bad trade)
[3.0, 92.0, 20.0]] # red (bad trade)
labels = ['green', 'green', 'red', 'red']
new_ trades = [[90.0, 10.0, 15.0],
[10.0, 98.0, 50.0]]
- Sample Output
['green', 'red']
- Techniques
We’re just looking for a simple, moderately effective, solution.
Test cases can be passed with fewer than 30 lines of code.
import sys, os
def melon_count(boxes,melons):
boxes.sort()
melons.sort()
result = 0
i = 0
j = 0
while(i != boxes.__len__() and j != melons.__len__()):
if boxes[i] >= melons[j]:
result = result+1
i = i+1
j= j+1
else:
i = i+1
return result