import operator


def select_survivors(snakes, num_survivors, survival_thresh):
    """ Picks the survivors that stay for next generation of snakes
    params:
        snakes: list, current generation of snakes of class Snake
        num_survivors: int, how many survivors there should be
        survival_thresh: float, selection probability threshold that survivors
                            must meet
    returns:
        list of survivors of class Snake,
        list of tuples of reverse sorted selection probabilities and
            the indices of the associated snakes
    """
    survivors = []
    select_probs = dict()
    for i in range(len(snakes)):
        select_probs[str(i)] = snakes[i].select_prob
    sorted_probs = sorted(select_probs.items(), key = operator.itemgetter(1),
            reverse = True)
    count = 0
    for i in range(len(sorted_probs)):
        if (survival_thresh <= sorted_probs[i][1]):
            if (count < num_survivors):
                survivors.append(snakes[int(sorted_probs[i][0])])
            snakes[int(sorted_probs[i][0])].selected = True
            count += 1
    return survivors, sorted_probs