/**
 * This function takes in a dictionary containing patient data and a list of error codes.
 * It iterates through each patient record in the dictionary and checks for any error code matches.
 * It returns a dictionary with the patient name as key and a list of errors as value.
 */
fun findPatientsWithErrors(patientData: Map<String, Map<String, Any>>, errorCodes: List<String>): Map<String, List<String>> {

    val errorPatients = mutableMapOf<String, List<String>>()

    for ((patient, data) in patientData) {
        val patientErrors = mutableListOf<String>()

        for (error in data["errors"] as List<String>) {
            if (error in errorCodes) {
                patientErrors.add(error)
            }
        }

        if (patientErrors.isNotEmpty()) {
            errorPatients[patient] = patientErrors
        }
    }

    return errorPatients
}

// Example usage
val patientData = mapOf(
    "John Doe" to mapOf(
        "age" to 35,
        "errors" to listOf("E001", "E003")
    ),
    "Jane Smith" to mapOf(
        "age" to 42,
        "errors" to listOf("E002", "E004", "E005")
    ),
    "David Johnson" to mapOf(
        "age" to 28,
        "errors" to listOf("E001", "E004")
    )
)
/**
 * This function takes in a dictionary containing patient data and a list of error codes.
 * It iterates through each patient record in the dictionary and checks for any error code matches.
 * It returns a dictionary with the patient name as key and a list of errors as value.
 */
fun findPatientsWithErrors(patientData: Map<String, Map<String, Any>>, errorCodes: List<String>): Map<String, List<String>> {
/**
 * This function takes in a dictionary containing patient data and a list of error codes.
 * It iterates through each patient record in the dictionary and checks for any error code matches.
 * It returns a dictionary with the patient name as key and a list of errors as value.
 */
fun findPatientsWithErrors(patientData: Map<String, Map<String, Any>>, errorCodes: List<String>): Map<String, List<String>> {

    val errorPatients = mutableMapOf<String, List<String>>()

    for ((patient, data) in patientData) {
        val patientErrors = mutableListOf<String>()

        for (error in data["errors"] as List<String>) {
            if (error in errorCodes) {
                patientErrors.add(error)
            }
        }

        if (patientErrors.isNotEmpty()) {
            errorPatients[patient] = patientErrors
        }
    }

    return errorPatients
}

// Example usage
val patientData = mapOf(
    "John Doe" to mapOf(
        "age" to 35,
        "errors" to listOf("E001", "E003")
    ),
    "Jane Smith" to mapOf(
        "age" to 42,
        "errors" to listOf("E002", "E004", "E005")
    ),
    "David Johnson" to mapOf(
        "age" to 28,
        "errors" to listOf("E001", "E004")
    )
)