Implement Safe Average Function
WaymoData Scientist · Online AssessmentLast updated
This question evaluates proficiency with basic programming fundamentals including numeric computation, handling edge cases such as empty inputs, input immutability, and data type consistency.
Write a Python function average(values) that returns the arithmetic mean of a list of numbers.
Requirements:
- If
valuesis empty, return0. - The input is a flat list of numeric values such as integers or floats.
- Do not mutate the input list.
Examples:
average([1, 2, 3])returns2.0average([-1, 1])returns0.0average([])returns0
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.