def _points_for_position(position, winner_points, num_tied):
    """
    Calculate the number of league points for a given position, allowing for
    ties.

    The number of points awarded decreases by two for each position below the
    winner, ties are resolved by sharing the points equally. For example, if a
    tied posiion would normally earn earn 8 points and there is a three-way tie
    for first place, each gets 6pts since (8+6+4)/3.

    While we could loop over the tied positions to share the points out, it's
    faster to just use maths to do it for us.

    Given that the general formula for the points at a given position is:

        (tied_pos_points + (tied_pos_points - 2) + (tied_pos_points - 4) ...)
        ---------------------------------------------------------------------
                                num_tied

    we start by pulling out the tied_pos_points:

        num_tied * tied_pos_points + ((-2) + (-4) ...)
        ----------------------------------------------
                          num_tied

    multiplying the right hand part through by -1 and knowing that the sum from
    1..(n-1) is (n(n-1))/2, we can rewrite that as:

        num_tied * tied_pos_points - num_tied * (num_tied - 1)
        -------------------------------------------------------
                               num_tied

    which obviously simplifies to:

        tied_pos_points - (num_tied - 1)

    which is what we use to calculate the points for given position.
    """

    # pos is 1-indexed, hence the subtraction
    points = winner_points - 2 * (position - 1)

    return points - (num_tied - 1)