def clamp(n, range):
    """
    Given a number and a range, return the number, or the extreme it is closest to.

    :param n: number
    :param range: tuple with min and max
    :return: number
    """
    minn, maxn = range
    return max(min(maxn, n), minn)