Created
March 3, 2016 16:44
-
-
Save jferris/460e3a1b7b1ba87953f1 to your computer and use it in GitHub Desktop.
Combine ranges
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
def combine_ranges(ranges) | |
if ranges.empty? | |
[] | |
else | |
first, *rest = ranges | |
rest.reduce([first]) do |result, current| | |
previous = result.pop | |
if previous.max + 1.day >= current.min | |
result << previous.min..current.min | |
else | |
result << previous << current | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment