Created
August 31, 2012 16:39
-
-
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
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
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