import typing


def indices_to_range(indices: "typing.Collection") -> str:
    """
    Convert a list of integers to the range format for sbatch and qsub.

    >>> indices_to_range([0, 1, 2, 3])
    '0-3'
    >>> indices_to_range([0, 10, 11, 12])
    '0,10-12'
    >>> indices_to_range([0, 1, 2, 10, 11, 12, 100, 101, 102])
    '0-2,10-12,100-102'

    See:

    - https://slurm.schedmd.com/sbatch.html
    - http://docs.adaptivecomputing.com/torque/4-2-7/help.htm#topics/commands/qsub.htm#-t

    """
    assert len(indices) == len(set(indices))

    ranges = []
    it = iter(indices)
    try:
        i = next(it)
    except StopIteration:
        return ""

    nonempty = True
    while nonempty:
        prev = beg = i
        for i in it:
            if i != prev + 1:
                break
            prev = i
        else:
            nonempty = False

        if beg == prev:
            ranges.append(str(beg))
        else:
            ranges.append("{}-{}".format(beg, prev))

    return ",".join(ranges)