Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
Write a function first_above(items, limit) that gives the position of the first item that is more than limit, and -1 if there is none. Test it with [72, 85, 64, 90, 58] and the limits 80, 90 and 100.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.
Plan. Go through the positions in order. Return at the first item that passes the test. If the loop ends, nothing passed.
Check. For 80: the first mark above 80 is 85, at position 1. For 90: 90 is not more than 90, so no mark passes: -1. For 100: -1.
Answer: 1, -1 and -1. (Notice the second: the test is >, so 90 is not above 90.)
Log in to join the discussion.
Loading…