gap is a short way to say “the difference between consecutive array elements”.
So using the example @RespiteSage provided earlier: n = [1, 3, 5, 9, 27] would produce:
n.gaps => [2, 2, 4, 18]
You can also have negative gap values, which tell you the data has changed slope up|down|flat.
[3, 6, 10, 17, 15, 11, 11, 13].gaps => [3, 4, 7, -2, -4, 0, 2]
[9, 2, 0, -2, -5, -1, -5, -13].gaps => [-7, -2, -2, -3, 4, -4, -8]
gap graphs are sometime called trend lines in economics.
So you’re a business, and you want a quick sense of how well you’re doing at any point.
You have daily sales figures (365 days of sales), so you do a trend (gap) analysis of the data.
Positive gaps means more sales (and how big) from the previous period, negative means a drop, and zero mean flat sales. You don’t need the absolute dollar sales, you’re just looking at your sales trends.
Here’s a short conceptualization (can be optimized) for min_max_gaps.
You don’t want an intermediate array (especially for huge data sets) just the min|max gaps.
def min_max_gaps(ary)
min, max, gap = 0, 0, 0
(ary.size - 1).times do |i|
gap = ary[i + 1] - ary[i]
min = gap if gap < min
max = gap if gap > max
end
{min, max}
end
Again, this kind of data analysis is frequently done in statistics, economics, number theory, etc.
Edit:
Now that there’s multi-threading, you can break the array into sections and parallel process them, and then select the min|max from each thread. Again, this is desirable for processing large data sets.