/**
 * Given a dictionary 'd" and a floating-point number 'x`, find and return the key in the dictionary that is closest to 'x`.
 * If there are multiple keys with the same minimum absolute difference to 'x`, return the first one encountered.
 *
 * @param d A dictionary with keys as strings and values as floats.
 * @param x A floating-point number.
 * @return The key in the dictionary that is closest to 'x`.
 *
 * Example:
 * findClosestKey(mapOf("A" to 1.2, "B" to 3.7, "C" to 2.9), 3.1) -> "C"
 */
fun findClosestKey(d: Map<String, Double>, x: Double): String? {

    var closestKey: String? = null
    var minDiff = Double.POSITIVE_INFINITY

    for ((key, value) in d) {
        val diff = Math.abs(value - x)

        if (diff < minDiff) {
            minDiff = diff
            closestKey = key
        }
    }

    return closestKey
}
/**
 * Given a dictionary 'd" and a floating-point number 'x`, find and return the key in the dictionary that is closest to 'x`.
 * If there are multiple keys with the same minimum absolute difference to 'x`, return the first one encountered.
 *
 * @param d A dictionary with keys as strings and values as floats.
 * @param x A floating-point number.
 * @return The key in the dictionary that is closest to 'x`.
 *
 * Example:
 * findClosestKey(mapOf("A" to 1.2, "B" to 3.7, "C" to 2.9), 3.1) -> "C"
 */
fun findClosestKey(d: Map<String, Double>, x: Double): String? {
/**
 * Given a dictionary 'd" and a floating-point number 'x`, find and return the key in the dictionary that is closest to 'x`.
 * If there are multiple keys with the same minimum absolute difference to 'x`, return the first one encountered.
 *
 * @param d A dictionary with keys as strings and values as floats.
 * @param x A floating-point number.
 * @return The key in the dictionary that is closest to 'x`.
 *
 * Example:
 * findClosestKey(mapOf("A" to 1.2, "B" to 3.7, "C" to 2.9), 3.1) -> "C"
 */
fun findClosestKey(d: Map<String, Double>, x: Double): String? {

    var closestKey: String? = null
    var minDiff = Double.POSITIVE_INFINITY

    for ((key, value) in d) {
        val diff = Math.abs(value - x)

        if (diff < minDiff) {
            minDiff = diff
            closestKey = key
        }
    }

    return closestKey
}