logo CodeStepByStep logo


Language/Type: Python lists

Write a function price_is_right that accepts a list of integers bids and an integer price as parameters. The function returns the element in the bids list that is closest in value to price without being larger than price. For example, if bids stores the elements [200, 300, 250, 999, 40], then price_is_right(bids, 280) should return 250, since 250 is the bid closest to 280 without going over 280. If all bids are larger than price, then your function should return -1.

You may assume there is at least 1 element in the list, and you may assume that the price and the values in bids will all be greater than or equal to 1. Do not modify the contents of the list passed to your function as a parameter.

Function: Write a Python function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.