LIVE
connecting to the live quote stream…
--:--:--UTC
easyMaths○
#31

Log vs simple returns

easyMathsacceptance —~10 min

Implement returns(prices, kind) — the per-period returns of a price series: kind = "simple" gives p_t / p_{t-1} - 1, kind = "log" gives ln(p_t / p_{t-1}). The list is one shorter than the prices.

Log returns add across time; simple returns add across a portfolio. Confusing the two is how a backtest gains a percent a year out of thin air. Return the values rounded to 4 decimal places.

example 1
input
prices=[100.0, 101.0, 99.0, 102.0], kind="simple"
output
[0.01, -0.0198, 0.0303]
example 2
input
prices=[100.0, 101.0, 99.0, 102.0], kind="log"
output
[0.01, -0.02, 0.0299]
constraints
  • 2 ≤ len(prices) ≤ 10_000
  • every price > 0
  • kind ∈ {simple, log}
teacher · 2 hints · the first is free
●solution.py· utf-8 · spaces:4
ln 1, col 1tab indent · esc leave editor
python 3.13 · pyodide
+ 6 hidden on submit
input · keyword arguments to returns()
prices=[100.0, 101.0, 99.0, 102.0], kind="simple"
expected
[0.01, -0.0198, 0.0303]

the judge calls returns() with exactly these inputs · outputs are compared to 6 decimal places

● standing bypython 3.13 in your browser · pyodide 0.28.3 · judged on the server to 6 dp⌘↵ run · ⌘⇧↵ submit
paper only · educational · not advice