Drill

ProblemsPython › finance

Average monthly figures for a year

mediumfinancePython

Twelve monthly totals summarise a year. Report the average monthly figure, dropping the fractional minor unit.

yearly_average(monthly_totals: list<int>) → int

Solve it in the editor →

Where you start

def yearly_average(monthly_totals: list[int]) -> int:
    

Worked examples

CallResult
yearly_average([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])1
yearly_average([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])6
yearly_average([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])0
yearly_average([100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100])100

Hint

Guard the length, sum the entries, divide by twelve.

Reference solution in Python
def yearly_average(monthly_totals: list[int]) -> int:
    if len(monthly_totals) < 12:
        return 0
    return sum(monthly_totals) // 12

The same problem in another language

More finance problems in Python