2013-02-28 91 views
3

我需要编写一个代码,其中包含Groovy中的一系列范围。我需要创建一个新的列表,所有的范围不重叠。删除范围列表中的重叠范围Groovy

例如,如果输入是:[13..15,14..16]

我应该能够创建具有任一[13..16]或[13..14列表, 14..16]

我真的很感激任何帮助。我写了下面的代码,但现在它不工作一个位:

def removeOverlapInRanges(ranges) 
{ 
    def cleanedRanges = [] 
    def overLapFound = false 
    def rangeIsClean = true 
    def test = "ranges" 
    ranges.each 
    { 
     range-> 

     def index = ranges.indexOf(range) 
     while (index < ranges.size() -1) 
     { 
      if (ranges.get(index + 1).disjoint(range) == false) 
      { 
       overLapFound = true 
       rangeIsClean = false 
       def nextRange = ranges.get(index + 1) 
       if (range.from > nextRange.from && range.to < nextRange.to) 
        cleanedRanges.add(range.from..range.to) 
       else if (range.from < nextRange.from && range.to < nextRange.to) 
        cleanedRanges.add(range.from..nextRange.to) 
       else if (range.from > nextRange.from && range.to > nextRange.to) 
        cleanedRanges.add(nextRange.from..range.to) 
      } 
      index = index + 1 
     } 
     if (rangeIsClean) 
      cleanedRanges.add(range) 

     rangeIsClean = true 

     test = test + cleanedRanges 
    } 
    cleanedRanges.add(0, cleanedRanges.get(cleanedRanges.size()-1)) 
    cleanedRanges.remove(cleanedRanges.size() - 1) 
    if (overLapFound) 
     return removeOverlapInRanges(cleanedRanges) 
    else 
     return cleanedRanges 
} 

我通过[12..13,17..19,18..22,17..19,22..23, 19..20]

作为回报我事先的任何输入了[12..13]

谢谢!

回答

4

我得到这个:

List<Range> simplify(List<Range> ranges) { 
    ranges.drop(1).inject(ranges.take(1)) { r, curr -> 
    // Find an overlapping range 
    def ov = r.find { curr.from <= it.to && curr.to >= it.from } 
    if(ov) { 
     ov.from = [ curr.from, ov.from ].min() 
     ov.to = [ curr.to, ov.to ].max() 
     simplify(r) 
    } 
    else { 
     r << curr 
    } 
    } 
} 

def ranges = [ 12..13, 17..19, 18..22, 17..19, 22..23, 19..20 ] 
assert simplify(ranges) == [ 12..13, 17..23 ] 

ranges = [ -2..3, -5..-2 ] 
assert simplify(ranges) == [ -5..3 ] 

ranges = [ 3..1, 1..5 ] 
assert simplify(ranges) == [ 5..1 ] // reversed as first range is reversed 

ranges = [ 1..5, 3..1 ] 
assert simplify(ranges) == [ 1..5 ] 

ranges = [ 1..5, 3..1, -1..-4 ] 
assert simplify(ranges) == [ 1..5, -1..-4 ] 

ranges = [ 1..5, -6..-4, 3..1, -1..-4 ] 
assert simplify(ranges) == [ 1..5, -6..-1 ] 

ranges = [ 1..3, 5..6, 3..5 ] 
assert simplify(ranges) == [ 1..6 ] 

虽然有可能是极端情况......所以我会做更多的测试...

+0

谢谢您的输入.. – user2119516 2013-03-07 08:34:10

0

的与创建独特的简单列表如下数字:

def ranges = [12..13, 17..19, 18..22,17..19, 22..23,19..20 ]; 
def range = ranges.flatten().unique().sort() 

这里有一个稍微不同的方法,产生了一些很好的辅助方法:

def parseToRangeString(range) 
{ 
    String result = ""; 
    range.eachWithIndex{cur,i-> 
     def nex = range[i+1] 
     def start = !result || result.endsWith(",") 
     def cont = cur == nex?.minus(1) 
     if (start && cont) //starting a new section and the next item continues this seq (starting a range = 1,10) 
      result += "$cur-" 
     else if (!cont && nex) //not continuing the seq and there are more nums to process (end with more = 6,8) 
      result += "$cur," 
     else if (!cont && !nex) //not continuing the seq but there are no more nums to process (very end = 11) 
      result += cur 
    } 
    return result 
} 
def toRange(rangeStr) 
{ 
    def ranges = rangeStr.split(",").collect{ 
     def range = it.split("-"); 
     new IntRange(range[0] as int, range[-1] as int) 
    } 
} 
List.metaClass.toRangeString = { 
    parseToRangeString(delegate) 
} 
List.metaClass.toRange = { 
    def rangeStr = parseToRangeString(delegate) 
    toRange(rangeStr) 
} 

def ranges = [12..13, 17..19, 18..22,17..19, 22..23,19..20 ]; 
def list = ranges.flatten().unique().sort() 
assert "12-13,17-23" == list.toRangeString() 
assert [12..13,17..23] == list.toRange(); 
+0

谢谢你帮我达成解决方案。万分感谢! – user2119516 2013-03-07 08:33:51

+0

你很受欢迎。不要忘记接受答案。 – purgatory101 2013-03-12 22:43:13