Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Using a generator function makes the task easy.</p> <pre><code>table = [(2,3),(5,6),(12,20),(21,25),(28,28),(35,48),(53,55)] def gaps_between(intervals): prec = intervals[0][1] + 1 for L,H in intervals: print '\nprec = %d (L,H) = (%d,%d)' % (prec,L,H) print 'prec = %d &lt;= L-1 = %d : %s' % (prec,L-1,prec&lt;=L) if prec&lt;=L-1: yield (prec,L-1) prec = H + 1 print 'next prec = %d' % prec holes = list(gaps_between(table)) print print 'table =',table print 'holes =',holes </code></pre> <p>In a former answer, I had used an iterator defined inside the generator.<br> To avoid this, I here above use a stratagem :<br> defining <code>first prec = first H = intervals[0][1]</code>.<br> Given the fact that <code>H&gt;=L</code> for every couple (L,H) , it leads to<br> <code>first H &gt; first L - 1</code> --> <code>first prec &gt; first L - 1</code>.<br> Hence the first test concerning the first interval is always False and the real process starts at the second interval.</p> <pre><code>prec = 3 (L,H) = (2,3) prec = 3 &lt;= L-1 = 1 : False next prec = 4 prec = 4 (L,H) = (5,6) prec = 4 &lt;= L-1 = 4 : True next prec = 7 prec = 7 (L,H) = (12,20) prec = 7 &lt;= L-1 = 11 : True next prec = 21 prec = 21 (L,H) = (21,25) prec = 21 &lt;= L-1 = 20 : True next prec = 26 prec = 26 (L,H) = (28,28) prec = 26 &lt;= L-1 = 27 : True next prec = 29 prec = 29 (L,H) = (35,48) prec = 29 &lt;= L-1 = 34 : True next prec = 49 prec = 49 (L,H) = (53,55) prec = 49 &lt;= L-1 = 52 : True next prec = 56 table = [(2, 3), (5, 6), (12, 20), (21, 25), (28, 28), (35, 48), (53, 55)] holes = [(4, 4), (7, 11), (26, 27), (29, 34), (49, 52)] </code></pre> <p>The result is correct:<br> - it gives the gap (4,4) between (2,3) and (5,6)<br> - there's no gap between (12,20) and (21,25)<br> - value 28 present in (28,28) isn't in any gap</p> <p>OP says that the intervals are non over-lapping and sorted.<br> However, the test <code>if prec&lt;=L-1</code> is mandatory, otherwise contiguous intervals give an error:<br> without this test, the result would contain<br> <code>....., (7, 11), (21, 20), (26, 27), .......</code></p> <p>. </p> <p>It happens that with this mandatory test, the following lists of intervals<br> <code>[[ 8, 9],[14, 18],[18, 32]]</code><br> <code>[[8, 9], [14, 18], [19, 20], [16, 21], [23, 32]]</code><br> that are over-lapping (not what OP says)<br> and that would give error without the test,<br> don't produce any error in fact.</p> <p>The rule for a list of intervals to give a correct list of gaps with my above code is that the intervals must be sorted along the second element.</p> <p>.</p> <p>Replacing <code>yield (prec,L-1)</code> with <code>yield range(prec,L)</code> will gives the gaps as ranges.</p> <p>Replacing <code>yield (prec,L-1)</code> with <code>holes.append((prec,L-1))</code>, for example, allows to write the code without a function.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload