The list average
- O(n) · Medium
- Full plan
- Python
- JavaScript
- lists
- loops
- decimals
Problem
Write a function that takes a list of numbers and returns their average: the sum of all of them divided by how many there are. Do not round it: if the average is 1.5, return 1.5.
You cannot divide by zero, so if the list is empty return 0.
Examples
[2, 4, 6] → 4
[1, 2] → 1.5
[10] → 10
Besides these, the challenge has hidden tests that are revealed when you submit your solution.
You start with this
Python
def list_average(numbers):
passJavaScript
function listAverage(numbers) {
}Solve this challenge
It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.