Average monthly figures for a year
Twelve monthly totals summarise a year. Report the average monthly figure, dropping the fractional minor unit.
- The list always carries twelve whole entries for a real year.
- Average is the total divided by 12, rounded down.
- A list with fewer than twelve entries is not a full year: return 0.
YearlyAverage(monthlyTotals: list<int>) → int
C# needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.
Where you start
public int YearlyAverage(List<int> monthlyTotals) {
}
Worked examples
| Call | Result |
|---|---|
YearlyAverage(new List<int> { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }) | 1 |
YearlyAverage(new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }) | 6 |
YearlyAverage(new List<int> { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }) | 0 |
YearlyAverage(new List<int> { 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 C#
public int YearlyAverage(List<int> monthlyTotals) {
if (monthlyTotals.Count < 12) return 0;
int sum = 0;
foreach (int v in monthlyTotals) sum += v;
return sum / 12;
}