human
Complete this python program:
def get_lines(ex, index_type):
assistant
#Here is the Python code you requested.

    """returns ``(lines, traces, rest)`` for an index type,
    where ``lines`` is the list of list of positions of a matrix line,
    ``traces`` is the list of list of traced matrix lines,
    ``rest`` is the rest of the elements ot the tensor.
    """
    def _join_lines(a):
        i = 0
        while (i < len(a)):
            x = a[i]
            xend = x[(-1)]
            xstart = x[0]
            hit = True
            while hit:
                hit = False
                for j in range((i + 1), len(a)):
                    if (j >= len(a)):
                        break
                    if (a[j][0] == xend):
                        hit = True
                        x.extend(a[j][1:])
                        xend = x[(-1)]
                        a.pop(j)
                        continue
                    if (a[j][0] == xstart):
                        hit = True
                        a[i] = (reversed(a[j][1:]) + x)
                        x = a[i]
                        xstart = a[i][0]
                        a.pop(j)
                        continue
                    if (a[j][(-1)] == xend):
                        hit = True
                        x.extend(reversed(a[j][:(-1)]))
                        xend = x[(-1)]
                        a.pop(j)
                        continue
                    if (a[j][(-1)] == xstart):
                        hit = True
                        a[i] = (a[j][:(-1)] + x)
                        x = a[i]
                        xstart = x[0]
                        a.pop(j)
                        continue
            i += 1
        return a
    arguments = ex.args
    dt = {}
    for c in ex.args:
        if (not isinstance(c, TensExpr)):
            continue
        if (c in dt):
            continue
        index_types = c.index_types
        a = []
        for i in range(len(index_types)):
            if (index_types[i] is index_type):
                a.append(i)
        if (len(a) > 2):
            raise ValueError(('at    most    two    indices    of    type    %s    allowed' % index_type))
        if (len(a) == 2):
            dt[c] = a
    lines = []
    traces = []
    traces1 = []
    for (p0, p1, c0, c1) in ex.dum_in_args:
        if (arguments[c0] not in dt):
            continue
     ...