/**
 * Calculate the average precision at k given a list of search results and the relevant items.
 *
 * @param results A list of search results.
 * @param relevantItems A set of relevant items.
 * @param k The value of k.
 * @return The average precision at k value.
 */
fun calculateAvgPrecisionAtK(results: List<String>, relevantItems: Set<String>, k: Int): Double {

    var kValue = k
    if (results.size < k) {
        kValue = results.size
    }

    var precisionSum = 0.0
    var relevantCount = 0

    for (i in 0 until kValue) {
        if (results[i] in relevantItems) {
            relevantCount++
            precisionSum += relevantCount.toDouble() / (i + 1)
        }
    }

    return precisionSum / kValue
}
/**
 * Calculate the average precision at k given a list of search results and the relevant items.
 *
 * @param results A list of search results.
 * @param relevantItems A set of relevant items.
 * @param k The value of k.
 * @return The average precision at k value.
 */
fun calculateAvgPrecisionAtK(results: List<String>, relevantItems: Set<String>, k: Int): Double {
/**
 * Calculate the average precision at k given a list of search results and the relevant items.
 *
 * @param results A list of search results.
 * @param relevantItems A set of relevant items.
 * @param k The value of k.
 * @return The average precision at k value.
 */
fun calculateAvgPrecisionAtK(results: List<String>, relevantItems: Set<String>, k: Int): Double {

    var kValue = k
    if (results.size < k) {
        kValue = results.size
    }

    var precisionSum = 0.0
    var relevantCount = 0

    for (i in 0 until kValue) {
        if (results[i] in relevantItems) {
            relevantCount++
            precisionSum += relevantCount.toDouble() / (i + 1)
        }
    }

    return precisionSum / kValue
}