from typing import Collection
from typing import Tuple
import statistics


def calc_lenient_interval_stats(all_intervals: Collection[float]) -> Tuple[float, float, float]:
    """Calculate min, max and mean values for the given time intervals, in a lenient manner (discarding outliers)"""

    # We need to be a little lenient on the min and max intervals: we are trying to achieve a constant rate rather than exact intervals.
    # To achieve this leniency, I'm going to sort the intervals and discard outliers (the highest and lowest 5% of values)
    discard_count = max(1, len(all_intervals) // 20)
    sorted_intervals = sorted(all_intervals)
    intervals = sorted_intervals[discard_count:-discard_count]
    min_interval = intervals[0]
    max_interval = intervals[-1]
    mean_interval = statistics.mean(intervals)
    return max_interval, mean_interval, min_interval