Skip to content

Instantly share code, notes, and snippets.

@brutuscat
Created August 31, 2012 16:39
Show Gist options
  • Save brutuscat/3555582 to your computer and use it in GitHub Desktop.
Save brutuscat/3555582 to your computer and use it in GitHub Desktop.
Completes an array with missing consecutive numbers. Kind of a linear interpolation. Read something more here http://stackoverflow.com/questions/4570755/calculate-missing-values-in-an-array-from-adjacent-values
list = [2, 3, 4, 6, 7, 9, 12]
[].tap{ |array|
[0, *list, 0].each_cons(3).map do |p, x, n|
array << x;
(n - x).times{|i| array << x + i if i > 0}
end
} # => [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment