In: Computer Science
You want to send pumpkins to some of your friends for Halloween! You look up shipping policies and find out that there are certain constraints involved when shipping packages. A pumpkin needs to weigh at least a certain amount in order to be eligible for shipping. At the same time, no pumpkin can weigh more than a certain amount due to packing restrictions.
However, you have already purchased a whole lot of pumpkins in varying sizes. You now want to find out which of these pumpkins are eligible for shipping. A pumpkin is eligible for shipping if it weighs at least as much as the minimum required weight, and not more than the maximum permitted shipping weight. You have the weights of all pumpkins in a Python list (let’s call this pumpkin_weights).Define a function get_shippable_pumpkins that takes 3 parameters: a list of numbers named pumpkin_weights, and two floats min_weight, and max_weight. The function should compare each value in the list with the minimum and maximum weights allowed (i.e., min_weight and max_weight), and return a list of pumpkins that are within the weight limits for shipping.
You may assume that the min_weight is always less than or equal to max_weight. In other words, the lower limit of weight will always be less than or equal to the upper limit.
Function Name: get_shippable_pumpkins |
Parameter: pumpkin_weights - A list of floats that represents the weights of pumpkins. min_weight - Minimum allowed weight for a pumpkin to be shippable (inclusive) max_weight - Maximum allowed weight for a pumpkin to be shippable (inclusive) |
Return: A list of pumpkins that are shippable. |
Description: Create and return shippable pumpkins, given a list of pumpkin weights and a minimum and maximum weight allowed for shipping. |
Examples: pumpkin_weights = [33.0, 36.3, 25.1, 29.4, 31.5] min_weight = 30.0 max_weight = 34.0 Returns: [33.0, 31.5] pumpkin_weights = [30.6, 21.1, 35.0] min_weight = 35.0 max_weight = 35.0 Returns: [35.0] pumpkin_weights = [16.9, 42.0, 25.5, 24.3, 32.6, 29.1, 30.3, 31.6] min_weight = 22.5 max_weight = 34.5 Returns: [25.5, 24.3, 32.6, 29.1, 30.3, 31.6] pumpkin_weights = [33.0, 36.0, 35.5, 33.6, 41.2] min_weight = 36.9 max_weight = 40.1 Returns: [ ] |
It is always a good practice to think about edge cases while writing programs. For example, if you find no pumpkins are eligible for shipping, you should return an empty list. Similarly, if the input list is empty, you should return an empty list again. Observe that if the two limits are identical (as in the second example), we are effectively looking for an exact weight for shipping-eligible pumpkins.
You should not take input from the user for this. While
testing your program, you may use your own test cases and pass them
to the function by calling the function from the python prompt
(lists and strings, loops). For this part of the assignment, we
only require the get_shippable_pumpkins function to be
written.
Answer:-
Python3 Program
def get_shippable_pumpkins(pumpkin_weights,min_weight,max_weight):
a=[]
for i in range(len(pumpkin_weights)):
if pumpkin_weights[i] >= min_weight and pumpkin_weights[i] <= max_weight:
a.append(pumpkin_weights[i])
return (a)
Output with the given test case
If you need any help in understanding, please comment your query.
I tried my best for this question, I hope you upvote my answer.
Thank You