Created
August 23, 2011 02:43
-
-
Save jish/1164205 to your computer and use it in GitHub Desktop.
Ranges
This file contains hidden or 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
| input = [1, 2, 3, 8, 9, 10, 21, 22, 23, 24] | |
| def scan(input) | |
| min, max = input[0], input[1] | |
| output = [] | |
| input.each_with_index do |current, index| | |
| next if index == 0 | |
| previous = input[index-1] | |
| if (current - previous == 1) | |
| # continue | |
| else | |
| output << "#{min}..#{previous}" | |
| min = current | |
| end | |
| end | |
| output << "#{min}..#{input.last}" | |
| output | |
| end | |
| output = scan(input) | |
| puts output.inspect | |
| # => ["1..3", "8..10", "21..24"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment