Advertisement
Guest User

Untitled

a guest
Jan 13th, 2023
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. # [1, 3], [2, 2], [3, 1] input
  2.  
  3.  
  4.  
  5. def load_truck(dimensions, truck_size):
  6. loads = []
  7.  
  8. for i, v in enumerate(dimensions):
  9. # (boxsize_count, items in box)
  10. tuple = (v[0], v[1])
  11. loads.append(tuple)
  12.  
  13. # sort biggest boxes first (contains more items)
  14. value_sorted = sorted(
  15. loads,
  16. key=lambda x: x[0]
  17. )
  18.  
  19. max_items = 0
  20. boxes_countdown = truck_size
  21. i = 0
  22. while (boxes_countdown > 0) and (i < len(value_sorted)):
  23. curr = value_sorted[i]
  24. total_boxes = curr[0]
  25. items_per_box = curr[1]
  26.  
  27. if boxes_countdown >= total_boxes:
  28. max_items = max_items + (total_boxes * items_per_box)
  29. boxes_countdown = boxes_countdown - total_boxes
  30. else:
  31. max_items = max_items + (boxes_countdown * items_per_box)
  32. boxes_countdown = boxes_countdown - total_boxes
  33.  
  34. i = i+1
  35.  
  36. return max_items
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43. def test_truck():
  44. tiny_truck_load = [[2, 2], [1, 3], [3, 1] ]
  45. tiny_truck_size = 4
  46.  
  47. ret = load_truck(tiny_truck_load, tiny_truck_size)
  48. print(ret)
  49.  
  50. assert ret == 8, "Should be 8"
  51.  
  52. if __name__ == "__main__":
  53. test_truck()
  54. print("Everything passed")
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement