Question

IN PYTHON Write a function called "def top_prices(prices, n):", which will take two arguments. The first...

IN PYTHON

Write a function called "def top_prices(prices, n):", which will take two arguments. The first argument is the list of prices, each price should be a number type (int, float). The second argument 'n' indicates top number of prices. For example, if n-3, this function should return a new list of the top 3 prices, sorted from lowest to highest. If n-10, this function should return a new list of the top 10 prices, sorted from lowest to highest. This function should not change the original 'prices' pass in This function should return a list

The following is the list of prices: [16.13, 32.2, 11.65, 39, 13.83, 25.33, 4.99, 13.1, 150, 37.81, 4.81, 4, 32 08, 12.66, 19.54, 48.52, 65.92, 18.2, 13.09, 19.32, 7.63, 2.23, 116.12, 3 66, 73.45, 54.71, 80.07, 15.99, 30.88, 10.91, 87.7, 6.34, 5.36, 20.66, 62 .05, 88.98, 4. 3, 63.42, 3.89, 34.01, 28.42, 4.69, 15.3, 55.22, 43.48, 11.7 3, 167.05, 11.17, 18.84, 44.31, 19.38, 29.38, 21.84, 57.59, 41.42, 23.91, 145.28, 14.76, 75.5, 2.32, 112.19, 38.87, 55.61, 13.35, 27.4, 6.49, 40.94, 8.66, 6.59, 45.73, 34.53, 8.47, 71.03, 108.39, 37.06]

0 0
Add a comment Improve this question Transcribed image text
Answer #1
def top_prices(prices, n):
    lst = [x for x in prices]
    lst.sort()
    return lst[-n:]


#Testing
prices = [16.13, 32.2, 11.65, 39, 13.83, 25.33, 4.99, 13.1, 150, 37.81, 4.81, 4, 32.08, 12.66, 19.54, 48.52, 65.92, 18.2, 13.09, 19.32, 7.63, 2.23, 116.12, 3.66, 73.45, 54.71, 80.07, 15.99, 30.88, 10.91, 87.7, 6.34, 5.36, 20.66, 62.05, 88.98, 4.3, 63.42, 3.89, 34.01, 28.42, 4.69, 15.3, 55.22, 43.48, 11.73, 167.05, 11.17, 18.84, 44.31, 19.38, 29.38, 21.84, 57.59, 41.42, 23.91, 145.28, 14.76, 75.5, 2.32, 112.19, 38.87, 55.61, 13.35, 27.4, 6.49, 40.94, 8.66, 6.59, 45.73, 34.53, 8.47, 71.03, 108.39, 37.06]
print(top_prices(prices, 3))
print(top_prices(prices, 10))

Output:

\color{red}Please\; upvote\;the \;solution \;if \;it \;helped.\;Thanks!

Add a comment
Know the answer?
Add Answer to:
IN PYTHON Write a function called "def top_prices(prices, n):", which will take two arguments. The first...
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
  • IN PYTHON Write a function called "def first 20 stock prices(svmbos, prices)" which will take two...

    IN PYTHON Write a function called "def first 20 stock prices(svmbos, prices)" which will take two arguments. symbols and prices'. Prices is list of prices in float. Write a 'for loop, to print to screen both stock symbols and stock prices, for first 20 stocks, in user friendly way. One example would be "Symbols: GE, Stock Price: 16.135" Each symbol and price for this symbol must be on the same line Price should only keep 2 decimal places, like 16.13...

  • Write a python function called "get_sma", that takes two input arguments, similar to the example provided....

    Write a python function called "get_sma", that takes two input arguments, similar to the example provided. First argument is the original prices of type list, and the 2nd input argument being the number of days we want to calculate SMA, like a 8 day sma or 35 day sma. This function should return a new list of sma price, based on the input day range, and should be able to handle any SMA calculation, like: sma200 for 200 days moving...

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
ADVERTISEMENT