First not smaller
Inputs
values is a list of 0 to 14 whole numbers in increasing order, each from 0 to 40. x is a whole number from 0 to 45.
Your tasks
- Set
lowto 0 andhightolen(values). - Use a
whileloop that runs whilelow < high. Look atmid, halfway between them (rounded down). Ifvalues[mid] < x, movelowtomid + 1. Otherwise movehightomid. - Complete
lowerBoundso it returnslow, the position of the first number that is at leastx. Return it, do not print it.
Example
With values = [1, 3, 3, 3, 8, 12] and x = 3, lowerBound returns 1.
Rules
- If every number is smaller than
x, returnlen(values). - Numbers may repeat. Return the earliest position.
Test data
TEST DATA 1: values is [1, 3, 3, 3, 8, 12], x is 3
TEST DATA 2: values is [2, 4], x is 9
Practice focus
Prefix sums, counting, binary search, best stretch and sorting, all written as plain loops. This is where loops turn into named algorithms. You have already written the loops.