easy +10 pts

Replace negatives with zero

Write a function that turns every negative number in a list into 0.

Write a function `replace_negatives(nums)` that takes a list of integers `nums` and returns a new list where every negative number has been replaced by `0`. Positive numbers, zero, and the relative order of items must stay the same. The input list itself must not be modified.

Constraints

Input is a list of integers, possibly empty. The length does not exceed 1,000,000. The function must not mutate the input list and must return a new list.

Example

>>> replace_negatives([1, -2, 3, -4, 5])
[1, 0, 3, 0, 5]
>>> replace_negatives([-1, 0, 2])
[0, 0, 2]
>>> replace_negatives([])
[]
10 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Create an empty result list.
Loop over each number and append 0 if it is negative, otherwise append the number.
Remember: do not modify the original list.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.