How to identify which numbers in a list are within each others' range

Arnaud Delobelle arnodel at googlemail.com
Fri Feb 1 15:28:33 EST 2008


On Feb 1, 8:17 pm, Karthik Gurusamy <kar1... at gmail.com> wrote:
[...]
> def strip_sort (a, b):
>     if a[0] < b[0]:
>         return -1
>     if a[0] > b[0]:
>         return 1
>     if a[0] == 'L': return -1
>     return 0
>
> def overlaps (strips_given):
>     s2 = [((s[0], 'L', i) , (s[1], 'R', i)) for i,s in
> enumerate(strips_given)]
>     strips = []
>     for s in s2:
>         strips.append(s[0])
>         strips.append(s[1])
>     clique = set()
>     edges = []
>     strips.sort(cmp=strip_sort)
>     for s in strips:
>         node = s[2]
>         if s[1] == 'L':
>             clique.add(node)
>         if s[1] == 'R':
>             new_edges = [(node, i) for i in clique if i != node]
>             edges += new_edges
>             clique.remove(node)
>     return edges

Interesting.  This is a long version of the algorithm I posted
earlier.

--
Arnaud




More information about the Python-list mailing list