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, 4, 5, 8, 9, 11, 12, 13, 15 ] | |
| # divide the input into runs of consecutive numbers | |
| last = input.first | |
| s = input.slice_before(lambda { |i| [i != last + 1, (last = i)].first }) | |
| # replace runs of 3 or more with first-last | |
| p s.map {|runs| runs.size < 3 ? runs : "#{runs.first}-#{runs.last}"} | |
| .flatten | |
| .join(', ') # => 1-5, 8, 9, 11-13, 15 |
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
| class Context | |
| def initialize(variables) | |
| variables.each do |name, value| | |
| instance_variable_set("@#{name}", value) | |
| end | |
| end | |
| def run(code, start_line) | |
| context = init_context | |
| load_variables(context) |