Question

Given a set, weights, and an integer desired_weight, remove the element of the set that is...

Given 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.

In Python.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

code

def result(weight,desired_weight):
#set a random number has smallest assuming this is the largest number possible.
#choose this so that for first iteration smallest isn't null and to make sure it is always large
smallest =10000
#for each loop to piterate over the set
for w in weight:
#check difference between element w and desired_weight
#take absolute value so that it helps with comparison
value = abs(w-desired_weight)
  
#check if difference is smaller than smallest if yes, make smallest equal to value
#store corresponding value from set in r
if value<smallest:
#print(w)
smallest = value
r = w
#remove the particular r from set and return the set
weight.remove(r)
return weight
  

  


v = {12, 19, 6, 14, 22, 7}
f=result(v,18)
print(f)
v = {2,4,6,8,10}
f=result(v,7)

print(f)

sample output

6, 7, 12, 14, 22 12, 4, 8, 10]

Add a comment
Know the answer?
Add Answer to:
Given a set, weights, and an integer desired_weight, remove the element of the set that is...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • For Python, May I please get help. Given a set, weights, and an integer desired_weight, remove...

    For Python, May I please get help. Given 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...

  • For Python 3 MPLS please 1. Given a variable, unproved_conjectures, that is associated with a dictionary...

    For Python 3 MPLS please 1. Given a variable, unproved_conjectures, that is associated with a dictionary that maps the common names of mathematical conjectures to the years when the conjectures were made, write a statement that deletes the entry for "Fermat's Last Theorem". 2. Given a dictionary d, create a new dictionary that reverses the keys and values of d. Thus, the keys of d become the values of the new dictionary and the values of d become the keys...

  • These are exercises in myprogramminglab for python and I think the simpler the code the better:...

    These are exercises in myprogramminglab for python and I think the simpler the code the better: 1.An arithmetic progression is a sequence of numbers in which the distance (or difference) between any two successive numbers is the same. This in the sequence 1, 3, 5, 7, ..., the distance is 2 while in the sequence 6, 12, 18, 24, ..., the distance is 6. Given the positive integer distance and the non-negative integer n, create a list consisting of the...

  • Question 1 To remove the underlining from an <a> element, you can use CSS to set...

    Question 1 To remove the underlining from an <a> element, you can use CSS to set its A. text-decoration property to none B. text-decoration property to off C. underline property to none D underline property to off Question 2 You can use the CSS list-style-type property to change A. the bullet style in an unordered list B the number style in an ordered list C the terms style in a description list Dthe bullet or number style in an unordered...

  • Question 1 To remove the underlining from an <a> element, you can use CSS to set...

    Question 1 To remove the underlining from an <a> element, you can use CSS to set its A. text-decoration property to none B. text-decoration property to off C. underline property to none D underline property to off Question 2 You can use the CSS list-style-type property to change A. the bullet style in an unordered list B the number style in an ordered list C the terms style in a description list Dthe bullet or number style in an unordered...

  • 8th-ed Chapter 03, Section 3.5, Problem 090 The following data give the weights (in pounds) lost...

    8th-ed Chapter 03, Section 3.5, Problem 090 The following data give the weights (in pounds) lost by 15 members of a health club at the end of 2 months after joining the club. 4 12 22 15 11 7 19 4 12 7 4 15 11 14 13 a. Compute the values of the three quartiles and the interquartile range. IQR= 8 b. Calculate the approximate value of the 82nd percentile. Enter an exact value. exact number, no tolerance C....

  • Consider the attack tree shown below. The integer outside a circle represents the node ID and integer inside a circle...

    Consider the attack tree shown below. The integer outside a circle represents the node ID and integer inside a circle represents the cost of an attack is attempted on the node. (1) Identify all the possible paths (i.e., the sequence of nodes in each path) for the attack tree. For clarity, label each path with a unique number. (8 marks) (2) For each of the paths identified, compute the cost for the attack leading to the root node Based on...

  • Refer to the data set in the accompanying table. Assume that the paired sample data is...

    Refer to the data set in the accompanying table. Assume that the paired sample data is a simple random sample and the differences have a distribution that is approximately normal. Use a significance level of 0.05 to test for a difference between the weights of discarded paper​ (in pounds) and weights of discarded plastic​ (in pounds). Household   Paper   Plastic 1 12.73   14.83 2 13.61   8.95 3   6.96   7.60 4 17.65   11.26 5 11.08   12.47 6 9.83   6.26 7 12.32   11.17...

  • Please Write the task in c++ Task A perfect number is an integer that is equal...

    Please Write the task in c++ Task A perfect number is an integer that is equal to the sum of its divisors (where 1 is considered a divisor). For example, 6 is perfect because its divisors are 1, 2, and 3, and 1 + 2 + 3 is 6. Similarly, 28 is perfect because it equals 1 + 2 + 4 + 7 + 14. A quite good number is an integer whose badness—the size of the difference between the...

  • PYTHON! The Sieve of Eratosthenes THANKS FOR HELP! A prime integer is any integer greater than...

    PYTHON! The Sieve of Eratosthenes THANKS FOR HELP! A prime integer is any integer greater than 1 that is evenly divisible only by itself and 1. The Sieve of Eratosthenes is a method of finding prime numbers. It operates as follows: Create a list with all elements initialized to 1 (true). List elements with prime indexes will remain 1. All other elements will eventually be set to zero. Starting with list element 2, every time a list element is found...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT