merging the time stamps of file if label is the same
<p>I have file with three columns, the Ist and 2nd column are the start and end of time while the 3rd is label. I want to merge the time stamps of consecutive rows ( 2 or more) if the label in the 3rd column is the same.</p>

<h1>inputs1:</h1>

<p><code>
0.000000        0.551875        x
0.551875        0.586875        x
0.586875        0.676188        t
0.676188        0.721875        t
0.721875        0.821250        t
0.821250        0.872063        p
0.872063        0.968625        q
0.968625        1.112250        q
</code></p>

<h1>inputs2:</h1>

<p><code>
0.000000        0.551875        x
0.551875        0.586875        x
0.586875        0.676188        t
0.676188        0.721875        t
0.721875        0.821250        t
0.821250        0.872063        p
0.872063        0.968625        q
0.968625        1.112250        q
1.112250        1.212250        x
1.212250        1.500000        x
</code></p>

<h1>inputs3:</h1>

<p><code>
0.000000        0.551875        x
0.551875        0.586875        x
0.586875        0.676188        t
0.676188        0.721875        t
0.721875        0.821250        t
0.821250        0.872063        oo
0.872063        0.968625        q
0.968625        1.112250        q
1.112250        1.212250        x
1.212250        1.500000        x
</code></p>

<h1>output</h1>

<p><code>
0.000000        0.586875        x
0.586875        0.821250        t
0.821250        0.872063        p
0.872063        1.112250        q
1.112250        1.500000        x
</code></p>

<p>In Groovy, given:</p>

<pre><code>def inputs = [
    [0.000000, 0.551875, 'x'],
    [0.551875, 0.586875, 'x'],
    [0.586875, 0.676188, 't'],
    [0.676188, 0.721875, 't'],
    [0.721875, 0.821250, 't'],
    [0.821250, 0.872063, 'p'],
    [0.872063, 0.968625, 'q'],
    [0.968625, 1.112250, 'q']
]
</code></pre>

<p>Just group them by the 3rd element in each list, and then for each group create a list containing;</p>

<ul>
<li>the first item of the first list</li>
<li>the second item of the last list</li>
<li>the key that they were grouped by</li>
</ul>

<p>Giving:</p>

<pre><code>def outputs = inputs.groupBy { it[2] }.collect { key, items -&gt;
    [items[0][0], items[-1][1], key]
}
</code></pre>

<p>Which results in:</p>

<pre><code>[[0.000000, 0.586875, 'x'],
 [0.586875, 0.821250, 't'],
 [0.821250, 0.872063, 'p'],
 [0.872063, 1.112250, 'q']]
</code></pre>

<h2>Gaps</h2>

<p>If your inputs can have gaps which you want to maintain, then you could try</p>

<pre><code>def inputs = [[0.000000, 0.551875, 'x'],
              [0.551875, 0.586875, 'x'], 
              [0.586875, 0.676188, 't'], 
              [0.676188, 0.721875, 't'], 
              [0.721875, 0.821250, 't'], 
              [0.821250, 0.872063, 'p'], 
              [0.872063, 0.968625, 'q'], 
              [0.968625, 1.112250, 'q'], 
              [1.112250, 1.551875, 'x'], 
              [1.551875, 2.000000, 'x']]

def outputs = inputs.inject([]) { accum, line -&gt;
    if(accum &amp;&amp; accum[-1][2] == line[2]) {
        accum[-1][1] = line[1]
    }
    else {
        accum &lt;&lt; line
    }
    accum
}
</code></pre>

<p>To give</p>

<pre><code>[[0.000000, 0.586875, 'x'],
 [0.586875, 0.821250, 't'],
 [0.821250, 0.872063, 'p'],
 [0.872063, 1.112250, 'q'],
 [1.112250, 2.000000, 'x']]
</code></pre>

<h2>Wildcards</h2>

<pre><code>def inputs = [[0.000000, 0.551875, 'x'],
              [0.551875, 0.586875, 'x'], 
              [0.586875, 0.676188, 't'], 
              [0.676188, 0.721875, 't'], 
              [0.721875, 0.821250, 't'], 
              [0.821250, 0.872063, 'oo'], 
              [0.872063, 0.968625, 'q'], 
              [0.968625, 1.112250, 'q'], 
              [1.112250, 1.551875, 'x'], 
              [1.551875, 2.000000, 'x']]

def coalesce(List inputs, String... wildcards) {
    inputs.inject([]) { accum, line -&gt;
        if(accum &amp;&amp; 
           (accum[-1][2] == line[2] || wildcards.contains(line[2]))) {
            accum[-1][1] = line[1]
        }
        else {
            accum &lt;&lt; line
        }
        accum
    }
}
</code></pre>

<p>then;</p>

<pre><code>def outputs = coalesce(inputs, 'oo')
</code></pre>

<p>gives:</p>

<pre><code>[[0.000000, 0.586875, 'x'],
 [0.586875, 0.872063, 't'],
 [0.872063, 1.112250, 'q'],
 [1.112250, 2.000000, 'x']]
</code></pre>

<p>And</p>

<pre><code>def outputs = coalesce(inputs, 'oo', 'q')
</code></pre>

<p>Gives</p>

<pre><code>[[0.000000, 0.586875, 'x'],
 [0.586875, 1.112250, 't'],
 [1.112250, 2.000000, 'x']]
</code></pre>

java python algorithm groovy