Your question is Find Problems in Code Sample. Take a moment with it on the right.
Talk me through your thinking if you like. When you're confident, submit your answer and I'll grade it like a real screen (7/10 or better passes).
Morningstar computes portfolio return summaries from a fund's historical daily prices. A junior engineer wrote the helper below to flag when a fund crosses a target return, and it's been quietly shipping numbers that don't match the fund's official reporting.
def compute_portfolio_returns(prices, holdings={}):
daily_returns = []
for i in range(len(prices)):
change = (prices[i] - prices[i - 1]) / prices[i - 1]
daily_returns.append(change)
average_return = sum(daily_returns) / len(daily_returns)
total_return = average_return * len(daily_returns)
holdings["last_run"] = total_return
if total_return == 0.15:
flag = "target hit"
else:
flag = "in progress"
return {"total_return": total_return, "flag": flag}
Find the problems in this code sample. Explain what each one causes and how you'd fix it, and note which ones are more about correctness versus style.