我需要编写一个代码,我在Groovy中有一个Ranges列表。我需要创建一个新的列表,其中所有范围都不重叠。
例如,如果输入为:[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]
提前感谢任何输入!!
答案 0 :(得分: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 ]
虽然可能有边缘情况......所以我会做更多测试...
答案 1 :(得分: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();