From 7375fdd7600fd83256d13644bc8b35dc2c71c2d0 Mon Sep 17 00:00:00 2001 From: Patrick Simianer Date: Sat, 19 Dec 2015 00:44:09 +0100 Subject: ruby: module, split range --- ruby/split_range.rb | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100755 ruby/split_range.rb (limited to 'ruby/split_range.rb') diff --git a/ruby/split_range.rb b/ruby/split_range.rb new file mode 100755 index 0000000..ee28c75 --- /dev/null +++ b/ruby/split_range.rb @@ -0,0 +1,27 @@ +#!/usr/bin/env ruby + + +a = (5..10) +b = (8..9) + +def split_range a, b, index=0 + return nil if a==b + aa = a.to_a + begin_split = b.first + end_split = b.last + + p1 = aa[0..aa.index([begin_split-1,aa.first].max)] + p2 = aa[aa.index([end_split+1, aa.last].min)..aa.last] + + if begin_split > a.first && end_split < a.last + return [(p1.first..p1.last), "X#{index}", (p2.first..p2.last)] + elsif begin_split == a.first + return ["X#{index}", (p2.first..p2.last)] + elsif end_split == a.last + return [(p1.first..p1.last), "X#{index}"] + end + return nil +end + +puts split_range(a, b).to_s + -- cgit v1.2.3