Revisions
-
darrencauthon revised this gist
Nov 26, 2012 . 1 changed file with 11 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -69,4 +69,15 @@ result[2].must_equal 1000..1000 end end describe "with dates" do it "should return ranges just like with integers" do result = ['2012-11-26', '2012-11-27', '2012-11-28', '2013-01-01', '2013-01-02', '2013-01-04']. map { |x| Date.parse(x) }.to_range result.count.must_equal 3 result[0].must_equal Date.parse('2012-11-26')..Date.parse('2012-11-28') result[1].must_equal Date.parse('2013-01-01')..Date.parse('2013-01-02') result[2].must_equal Date.parse('2013-01-04')..Date.parse('2013-01-04') end end end -
darrencauthon revised this gist
Nov 26, 2012 . 2 changed files with 77 additions and 26 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,28 +1,7 @@ class Array def to_range highs = self.select { |x| self.include?(x+1) == false }.sort_by { |x| x } lows = self.select { |x| self.include?(x-1) == false }.sort_by { |x| x } (0...lows.count).map { |i| lows[i]..highs[i] } end end 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,72 @@ require 'minitest/spec' require 'minitest/autorun' require_relative 'to_range' describe '#to_range' do describe "empty set" do it "should return an empty set" do [].to_range.count.must_equal 0 end end describe "one item" do [1, 2, 1001].each do |value| it "should return one range" do result = [value].to_range result.count.must_equal 1 result[0].must_equal value..value end end end describe "two consecutive items" do [[1,2], [3,4], [10000, 10001]].each do |array| it "should return one range" do result = [array[0],array[1]].to_range result.count.must_equal 1 result[0].must_equal array[0]..array[1] end end end describe "two nonconsecutive items" do [[1,3], [3, 5], [566, 1001]].each do |array| it "should return two ranges for each item" do result = array.to_range result.count.must_equal 2 result[0].must_equal array[0]..array[0] result[1].must_equal array[1]..array[1] end end end describe "three consecutive items" do [[1,2,3], [2,3,4], [100,101,102]].each do |array| it "should return one range" do result = array.to_range result.count.must_equal 1 result[0].must_equal array.first..array.last end end end describe "joe's example" do it "should return one range" do result = [1,2,3,4,5,100,101,102,400,401,402,403,404,405,406].to_range result.count.must_equal 3 result[0].must_equal 1..5 result[1].must_equal 100..102 result[2].must_equal 400..406 end end describe "unordered example" do it "should return one range" do result = [4,3,100,102,1000,1,2,101].to_range result.count.must_equal 3 result[0].must_equal 1..4 result[1].must_equal 100..102 result[2].must_equal 1000..1000 end end end -
jmeirow created this gist
Nov 26, 2012 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,28 @@ require 'pp' require 'date' def to_range low, high, input, output if high > input.length return output.each_slice(2).map {|a| a }.map { |x| x[0]..x[1]} end if low == 0 output = Array.new output << input[low] else if input[low]+1 != input[high] output << input[low] output << input[high] if input[high] end end to_range (low+1), (high+1), input, output end input = [1,2,3,4,5,100,101,102,400,401,402,403,404,405,406] pp to_range 0, 1, input, nil # => [1..5, 100..102, 400..406]