import itertools


def get_group_i(cubes: list) -> list:
    """Given a list of CCDIDs, return a list of lists where each list contains
    continuous CCD number range indexes that have good statistics."""
    cubes.sort()
    good_indexes = list()
    for i, c in enumerate(cubes):
        if c.rstats is not None:
            if i + 1 < len(cubes):
                if cubes[i + 1].lstats is not None:
                    good_indexes.append(int(c.ccdnumber))
            else:
                good_indexes.append(int(c.ccdnumber))

    good_indexes.sort()
    # print(f'good indexes: {good_indexes}')
    pairs = list()
    for offset, group in itertools.groupby(
        good_indexes, lambda x, c=itertools.count(): next(c) - x
    ):
        pairs.append((offset, list(group)))
    return pairs