Before choosing a linear or log axis, summarise how a series grows: each period's growth, the compound rate and how long it takes to double.
What to build
A program that summarises the growth of a series.
Requirements
- Print
growthfollowed by each period's growth in percent with a sign and one decimal (+10.0%). - Print
CAGR X%(one decimal), thendoubling Y periods(one decimal) if the compound rate is over 0, otherwisedoubling never. - Finally print
constant yesif all period growth rates are within 0.1 percentage points of each other, otherwiseconstant no.
Input
One line: the values, at least two, all over 0.
Output
Four lines.
Constraints
2 to 100 values.
Example
Input:
100 110 121 133.1
Output:
growth +10.0% +10.0% +10.0%
CAGR 10.0%
doubling 7.3 periods
constant yes
Why it works
Compounding means the right summary is the constant rate that turns the first value into the last, and when growth is constant a log axis shows it as a straight line, which is why the check matters for choosing the axis.
Examples
100 110 121 133.1
growth +10.0% +10.0% +10.0% CAGR 10.0% doubling 7.3 periods constant yes
50 100
growth +100.0% CAGR 100.0% doubling 1.0 periods constant yes
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
The compound rate is (last ÷ first) ** (1 ÷ periods) − 1, where periods is the number of values minus one.
Hint 2
Doubling time is ln 2 ÷ ln(1 + rate); use math.log.
Hint 3
Growth is constant when the largest and smallest period growth differ by at most 0.1 percentage points.
Solution
Show one correct solution (try it yourself first)
import math
values = [float(v) for v in input().split()]
growth = [(b - a) / a * 100 for a, b in zip(values, values[1:])]
print("growth", " ".join(f"{g:+.1f}%" for g in growth))
rate = (values[-1] / values[0]) ** (1 / (len(values) - 1)) - 1
print(f"CAGR {rate * 100:.1f}%")
if rate > 0:
print(f"doubling {math.log(2) / math.log(1 + rate):.1f} periods")
else:
print("doubling never")
print("constant", "yes" if max(growth) - min(growth) <= 0.1 else "no")