Answer:
In the question stated a we declared a set named as weights also an integer variable was the desired_weight
In python A set is regarded as {12, 19, 6, 14, 22, 7}
Step-by-step explanation:
Solution
The python program code is executed below:
# Declare a tuple
weights = (12, 19, 6, 14, 22, 7)
# Declare an integer variable to store desired weight.
desired_weight = 18
# Declare an integer variable to store actual_weight weight
actual_weight = weights[0]
# Create a for-loop to read each value in the tuple.
for each_weight in weights:
# Create an if-statement to find the that is
# closest weight but not greater than desired weight.
if(each_weight > actual_weight and each_weight < desired_weight):
actual_weight = each_weight
weights=list(weights)
# Delete the actual weight
weights.remove(actual_weight)
weights=tuple(weights)
# Display the actual weight
print("Actual Weight:",actual_weight)
# Display the resulted tuple.
print("Resulted set:",weights)
This program here is to declare a set which is as follows:
# Declare a set
weights = {12, 19, 6, 14, 22, 7}
# Declare an integer variable to store desired weight.
desired_weight = 18
# Declare an integer variable to store actual weight
# Assign first index element using list.
actual_weight = list(weights)[0]
# Create a for-loop to read each value in the tuple.
for each_weight in weights:
# Create an if-statement to find the that is
# closest weight but not greater than desired weight.
if(each_weight > actual_weight and each_weight < desired_weight):
actual_weight = each_weight
# Delete the actual weight using discard() method.
weights.discard(actual_weight)
# Display the actual weight
print("Actual Weight:",actual_weight)
# Display the resulted tuple.
print("Resulted set:",weights)