easy +8 pts

Normalize Array to Zero-One Range

Scale numbers to [0,1] using min-max normalization

Write a function `normalize_zero_one(arr)` that takes a list of integers or floats and returns a new list where each element is scaled to the range [0,1] using min-max normalization. The formula is: `(x - min(arr)) / (max(arr) - min(arr))` If all elements are identical (so that max = min), return a list of zeros with the same length. The function should not modify the input list. You may assume the list is non-empty.

Constraints

The input list will contain at least one element. All elements are integers or floats. The length of the list is at most 10^6. The solution should run in O(n) time and use O(n) extra space (including the output).

Example

>>> normalize_zero_one([1, 2, 3])
[0.0, 0.5, 1.0]
>>> normalize_zero_one([10, 20, 30, 40])
[0.0, 0.3333333333333333, 0.6666666666666666, 1.0]
>>> normalize_zero_one([5, 5, 5])
[0.0, 0.0, 0.0]
>>> normalize_zero_one([0])
[0.0]
8 points ~12 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Find the minimum and maximum of the array once.
Handle the case where max equals min separately.
Use a list comprehension to build the result.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.