Spread of the list
Inputs
values is a list of 1 to 12 whole numbers, each from -50 to 50.
Your tasks
- Create
lowandhighand set both to the first number invalues. - Use one loop over
valuesthat updateslowwhen it sees something smaller andhighwhen it sees something larger. - Work out
high - low. - Complete
spreadso it returnshigh - low. Return it, do not print it.
Example
With values = [4, 11, -3, 8], spread returns 14.
Rules
- Use a single loop, not one loop for each.
Test data
TEST DATA 1: values is [4, 11, -3, 8]
TEST DATA 2: values is [5]
Practice focus
One pass over a list to add, count, find the biggest and the smallest. Scanning an array once and keeping a running answer is the backbone of linear-time solutions.