Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # [1, 3], [2, 2], [3, 1] input
- def load_truck(dimensions, truck_size):
- loads = []
- for i, v in enumerate(dimensions):
- # (boxsize_count, items in box)
- tuple = (v[0], v[1])
- loads.append(tuple)
- # sort biggest boxes first (contains more items)
- value_sorted = sorted(
- loads,
- key=lambda x: x[0]
- )
- max_items = 0
- boxes_countdown = truck_size
- i = 0
- while (boxes_countdown > 0) and (i < len(value_sorted)):
- curr = value_sorted[i]
- total_boxes = curr[0]
- items_per_box = curr[1]
- if boxes_countdown >= total_boxes:
- max_items = max_items + (total_boxes * items_per_box)
- boxes_countdown = boxes_countdown - total_boxes
- else:
- max_items = max_items + (boxes_countdown * items_per_box)
- boxes_countdown = boxes_countdown - total_boxes
- i = i+1
- return max_items
- def test_truck():
- tiny_truck_load = [[2, 2], [1, 3], [3, 1] ]
- tiny_truck_size = 4
- ret = load_truck(tiny_truck_load, tiny_truck_size)
- print(ret)
- assert ret == 8, "Should be 8"
- if __name__ == "__main__":
- test_truck()
- print("Everything passed")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement