From a6bb9f91eedddbc250af0bf1adaabe6c9571ac46 Mon Sep 17 00:00:00 2001 From: Gagandeep Singh Date: Wed, 11 Dec 2024 20:15:22 -0500 Subject: [PATCH] Update 32_stonks_solution_2.py Another solution that can be added, if you guys would like to. I used this and maybe other users would find it simpler than the better but complex first solution using for loops. No issues if you don't. --- 6-functions/32_stonks.py | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/6-functions/32_stonks.py b/6-functions/32_stonks.py index d2ba9d6..9ee5e88 100644 --- a/6-functions/32_stonks.py +++ b/6-functions/32_stonks.py @@ -2,22 +2,16 @@ # Codédex stock_prices = [34.68, 36.09, 34.94, 33.97, 34.68, 35.82, 43.41, 44.29, 44.65, 53.56, 49.85, 48.71, 48.71, 49.94, 48.53, 47.03, 46.59, 48.62, 44.21, 47.21] - -def price_at(i): - return stock_prices[i-1] - +def price_at(x): + price_that_day = stock_prices[x] + return price_that_day def max_price(a, b): - mx = 0 - for i in range(a, b + 1): - mx = max(mx, price_at(i)) - return mx - + max_stock_price = max(stock_prices[a:b+1]) + return max_stock_price def min_price(a, b): - mn = price_at(a) - for i in range(a, b + 1): - mn = min(mn, price_at(i)) - return mn - -print(max_price(1, 15)) -print(min_price(5, 10)) -print(price_at(3)) + min_stock_price = min(stock_prices[a:b+1]) + return min_stock_price +print("The stock price for the day is " + str(price_at(1))) +print("The maximum stock price is " + str(max_price(0,3))) +print("The minimum stock price is " + str(min_price(0,3))) +print(stock_prices[0:3])