3

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]を得ました

ご意見をお寄せいただきありがとうございます。

4

2 に答える 2

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 ]

おそらくエッジケースがありますが...だから私はもう少しテストを行います...

于 2013-02-28T12:28:21.397 に答える
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();
于 2013-02-28T22:47:50.003 に答える