summaryrefslogtreecommitdiff
path: root/ruby/split_range.rb
blob: ee28c75287df6aa09f77fdf0d69455948a8eb7ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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