2013-02-28 122 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