First negative
Inputs
values is a list of 0 to 14 whole numbers, each from -10 to 30.
Your tasks
- Use a loop over the positions of
values. - As soon as you meet a negative number, return its position straight away.
- Complete
firstNegativeIndexso it returns the position of the first negative number, or -1. Return it, do not print it.
Example
With values = [4, 8, -2, 5, -9], firstNegativeIndex returns 2.
Rules
- Positions start at 0.
- If there is no negative number, return -1.
Test data
TEST DATA 1: values is [4, 8, -2, 5, -9]
TEST DATA 2: values is [1, 2, 3]
Practice focus
While loops and early exits, for when you don't know how many rounds you need. Binary search, the Euclidean algorithm and graph search are all while loops with a clear stopping rule.