/**
 * A function that simulates population growth within a while loop and allows for appending and removing elements.
 *
 * @param population A list containing the initial population of a group of individuals.
 * @param growthRate The rate at which the population grows over time.
 * @param maxPopulation The maximum population size allowed.
 * @return The updated population list after simulating growth according to the growth rate.
 */
fun calculatePopulationGrowth(population: MutableList<Double>, growthRate: Double, maxPopulation: Int): MutableList<Double> {

    while (population.size < maxPopulation) {
        val lastPopulation = population.last()
        val nextPopulation = lastPopulation * growthRate

        // Check if the next population exceeds the maximum allowed and remove elements if necessary
        if (nextPopulation > maxPopulation) {
            // Calculate the average value of the current and next population
            val avgPopulation = (lastPopulation + nextPopulation) / 2
            // Check each element in population and remove the ones above the average
            population.removeIf { it >= avgPopulation }
            break
        }

        // Append the calculated next population to the population list
        population.add(nextPopulation)
    }

    return population
}
/**
 * A function that simulates population growth within a while loop and allows for appending and removing elements.
 *
 * @param population A list containing the initial population of a group of individuals.
 * @param growthRate The rate at which the population grows over time.
 * @param maxPopulation The maximum population size allowed.
 * @return The updated population list after simulating growth according to the growth rate.
 */
fun calculatePopulationGrowth(population: MutableList<Double>, growthRate: Double, maxPopulation: Int): MutableList<Double> {
/**
 * A function that simulates population growth within a while loop and allows for appending and removing elements.
 *
 * @param population A list containing the initial population of a group of individuals.
 * @param growthRate The rate at which the population grows over time.
 * @param maxPopulation The maximum population size allowed.
 * @return The updated population list after simulating growth according to the growth rate.
 */
fun calculatePopulationGrowth(population: MutableList<Double>, growthRate: Double, maxPopulation: Int): MutableList<Double> {

    while (population.size < maxPopulation) {
        val lastPopulation = population.last()
        val nextPopulation = lastPopulation * growthRate

        // Check if the next population exceeds the maximum allowed and remove elements if necessary
        if (nextPopulation > maxPopulation) {
            // Calculate the average value of the current and next population
            val avgPopulation = (lastPopulation + nextPopulation) / 2
            // Check each element in population and remove the ones above the average
            population.removeIf { it >= avgPopulation }
            break
        }

        // Append the calculated next population to the population list
        population.add(nextPopulation)
    }

    return population
}