/**
 * Given a map representing a farm with different types of animals and a list of new animals,
 * append the new animals to the corresponding animal type in the farm map.
 *
 * @param farm A map representing a farm with animal types as keys and lists of animals as values.
 * @param newAnimals A list of new animals to be added to the farm.
 * @return The updated farm map after appending the new animals.
 */
fun addAnimalsToFarm(farm: MutableMap<String, MutableList<String>>, newAnimals: List<String>): MutableMap<String, MutableList<String>> {

    for (animal in newAnimals) {
        // Extract the animal type from the animal name
        val animalType = animal.split(" ")[1]

        // Check if the animal type exists in the farm map
        if (farm.containsKey(animalType)) {
            // Append the new animal to the corresponding animal type in the farm map
            farm[animalType]?.add(animal)
        } else {
            // Add a new animal type to the farm map with the new animal as the first item
            farm[animalType] = mutableListOf(animal)
        }
    }
    return farm
}

// Example usage
val farm = mutableMapOf(
    "Horse" to mutableListOf("Coco", "Polly")
)
/**
 * Given a map representing a farm with different types of animals and a list of new animals,
 * append the new animals to the corresponding animal type in the farm map.
 *
 * @param farm A map representing a farm with animal types as keys and lists of animals as values.
 * @param newAnimals A list of new animals to be added to the farm.
 * @return The updated farm map after appending the new animals.
 */
fun addAnimalsToFarm(farm: MutableMap<String, MutableList<String>>, newAnimals: List<String>): MutableMap<String, MutableList<String>> {
/**
 * Given a map representing a farm with different types of animals and a list of new animals,
 * append the new animals to the corresponding animal type in the farm map.
 *
 * @param farm A map representing a farm with animal types as keys and lists of animals as values.
 * @param newAnimals A list of new animals to be added to the farm.
 * @return The updated farm map after appending the new animals.
 */
fun addAnimalsToFarm(farm: MutableMap<String, MutableList<String>>, newAnimals: List<String>): MutableMap<String, MutableList<String>> {

    for (animal in newAnimals) {
        // Extract the animal type from the animal name
        val animalType = animal.split(" ")[1]

        // Check if the animal type exists in the farm map
        if (farm.containsKey(animalType)) {
            // Append the new animal to the corresponding animal type in the farm map
            farm[animalType]?.add(animal)
        } else {
            // Add a new animal type to the farm map with the new animal as the first item
            farm[animalType] = mutableListOf(animal)
        }
    }
    return farm
}

// Example usage
val farm = mutableMapOf(
    "Horse" to mutableListOf("Coco", "Polly")
)