import statistics


def median(results, extractor, solved):
    """ Calculate median of numerical field over all tries.
    
    Args:
        results: dictionary mapping test case IDs to lists of tries
        extractor: function for extracting value of interest
        solved: whether to consider solved test cases only
    
    Returns:
        average of field over all relevant tries
    """
    values = []
    for tries in results.values():
        for one_try in tries:
            if not solved or one_try['similarity'] == 1.0:
                value = extractor(one_try)
                if value is not None:
                    values += [value]
    return statistics.median(values) if values else -1