/**
 * This function takes patient temperature readings and calculates the average temperature excluding any outliers.
 *
 * @param patientData A nested dictionary containing patient IDs as keys, and their temperature readings as
 * values, which are also dictionaries with dates as keys and temperatures as values.
 * @return A dictionary containing patient IDs as keys and their average temperature as values.
 */
fun calculateAvgTemperature(patientData: Map<String, Map<String, Int>>): Map<String, Double> {

    val avgTemperatures = mutableMapOf<String, Double>()

    for ((patientId, readings) in patientData) {
        var totalTemperature = 0
        var totalReadings = 0

        for ((_, temperature) in readings) {
            if (temperature < 35 || temperature > 39) {
                continue
            }

            totalTemperature += temperature
            totalReadings++
        }

        if (totalReadings != 0) {
            val avgTemperature = totalTemperature.toDouble() / totalReadings
            avgTemperatures[patientId] = avgTemperature
        }
    }

    return avgTemperatures
}

// Example usage:
val data = mapOf(
    "patient1" to mapOf("2021-01-01" to 37, "2021-01-02" to 36, "2021-01-03" to 40),
    "patient2" to mapOf("2021-01-01" to 38, "2021-01-02" to 39, "2021-01-03" to 34)
)
/**
 * This function takes patient temperature readings and calculates the average temperature excluding any outliers.
 *
 * @param patientData A nested dictionary containing patient IDs as keys, and their temperature readings as
 * values, which are also dictionaries with dates as keys and temperatures as values.
 * @return A dictionary containing patient IDs as keys and their average temperature as values.
 */
fun calculateAvgTemperature(patientData: Map<String, Map<String, Int>>): Map<String, Double> {
/**
 * This function takes patient temperature readings and calculates the average temperature excluding any outliers.
 *
 * @param patientData A nested dictionary containing patient IDs as keys, and their temperature readings as
 * values, which are also dictionaries with dates as keys and temperatures as values.
 * @return A dictionary containing patient IDs as keys and their average temperature as values.
 */
fun calculateAvgTemperature(patientData: Map<String, Map<String, Int>>): Map<String, Double> {

    val avgTemperatures = mutableMapOf<String, Double>()

    for ((patientId, readings) in patientData) {
        var totalTemperature = 0
        var totalReadings = 0

        for ((_, temperature) in readings) {
            if (temperature < 35 || temperature > 39) {
                continue
            }

            totalTemperature += temperature
            totalReadings++
        }

        if (totalReadings != 0) {
            val avgTemperature = totalTemperature.toDouble() / totalReadings
            avgTemperatures[patientId] = avgTemperature
        }
    }

    return avgTemperatures
}

// Example usage:
val data = mapOf(
    "patient1" to mapOf("2021-01-01" to 37, "2021-01-02" to 36, "2021-01-03" to 40),
    "patient2" to mapOf("2021-01-01" to 38, "2021-01-02" to 39, "2021-01-03" to 34)
)