easy +10 pts

Range of values

Write a function that returns the difference between the largest and smallest numbers in a list.

Write a function `range_of_values(numbers)` that takes a list of numbers (int or float) and returns the range of the values, defined as the maximum value minus the minimum value. If the list is empty, return `0`. The list may contain negative numbers, duplicates, and values of mixed types (ints and floats). You can assume all elements are numbers. The order of elements does not matter. You must implement the function exactly with the given name and parameter.

Constraints

- 0 <= len(numbers) <= 10^5 - Values can be any real numbers (int or float). - The function should run in O(n) time and use O(1) extra space.

Example

>>> range_of_values([3, 1, 4, 1, 5])
4
>>> range_of_values([-5, 5])
10
>>> range_of_values([])
0
>>> range_of_values([2.5, 2.5])
0.0
10 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Think about how to find the maximum and minimum of a list in one pass.
If the list is empty, what should you return? Check that case first.
The range is simply max_value - min_value.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.