From cef4f6959a5f1812718217b62092e87a8c2e11ce Mon Sep 17 00:00:00 2001 From: LegendJurai Date: Wed, 26 Feb 2025 09:57:40 +0100 Subject: [PATCH] Update 32_stonks.py My solution for this exercise was this code. It's shorter and it works. Just wanted to ask if it's also a good way to fulfill this exercise? --- 6-functions/32_stonks.py | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/6-functions/32_stonks.py b/6-functions/32_stonks.py index d2ba9d6..8f636c6 100644 --- a/6-functions/32_stonks.py +++ b/6-functions/32_stonks.py @@ -3,21 +3,16 @@ 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): + return(stock_prices[x - 1]) def max_price(a, b): - mx = 0 - for i in range(a, b + 1): - mx = max(mx, price_at(i)) - return mx + return(max(stock_prices[a - 1 : b])) def min_price(a, b): - mn = price_at(a) - for i in range(a, b + 1): - mn = min(mn, price_at(i)) - return mn + return(min(stock_prices[a - 1 : b])) -print(max_price(1, 15)) -print(min_price(5, 10)) print(price_at(3)) +print(max_price(2, 7)) +print(min_price(1, 2)) +