import math


def discounted_cumulative_gain(relevance_list, p):
    """
    Method that given a list of relevant documents (in this case the ranking positions),
    calculates the discounted cumulative gain for the list at position p. In order for this measure to be effective,
    the final score should be normalized (Dividing the ideal dcg by the dcg). DCG is defined as:
    dcg_p = sum(from i=1, to p) rel_i/log2(i+1)
    :param relevance_list list of elements to consider
    :param p position to which we want to calculate the score
    """
    dcg_p = 0
    i = 1
    for element in relevance_list:
        if i <= p:
            dcg_p += element/math.log2(i+1)
            i += 1
        else:
            break
    return dcg_p