Created
December 30, 2017 21:49
-
-
Save doyle/b55e1b288340e9d06daeb9b37d1bc475 to your computer and use it in GitHub Desktop.
Given a sorted array of numbers will group the numbers so long as they are within a given delta of each other. That is to say will group numbers that are close but once a gap is found a new group is started.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
times = [1,2,4,5,10,12,14,20,22,23,24] | |
@group = 0 | |
@delta = 3 | |
def group(times) | |
times.group_by do |item| | |
index = times.index(item) | |
if index <= times.size - 2 | |
a = index + 1 | |
b = index | |
else | |
a = index | |
b = index - 1 | |
end | |
if times[a] - times[b] < @delta | |
@group | |
else | |
@group = @group + 1 | |
@group - 1 | |
end | |
end | |
end | |
puts group times |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment