Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

pythonGiven a set, weights , and an integer desired_weight , remove the element

ID: 3758357 • Letter: P

Question

pythonGiven a set, weights, and an integer desired_weight, remove the element of the set that is closest to desired_weight (the closest element can be less than, equal to OR GREATER THAN desired_weight), and associate it with the variable  actual_weight. For example, if weights is (12, 19, 6, 14, 22, 7) and desired_weight is 18, then the resulting set would be (12, 6, 14, 22, 7) and actual_weight would be 19. If there is a tie, the element LESS THAN desired_weight is to be chosen. Thus if the set is (2, 4, 6, 8, 10) and desired_weight is 7, the value chosen would be 6, not 8. Assume there is at least one value in the set.

Explanation / Answer

desired_weight = weights[0]
diff = inf
actual_weight = 0
for x in weights:
if x < desired_weight and (x-desired_weight) < diff:
diff = x - desired_weight
actual_weight = x
weights.remove(actual_weight)