I am trying to make a Python program to solve the following problem:
A large number of ants live on the island of Eden and reproduce at a rate of 40% per month. There is also an anteater on the island that eats 7,000 ants at the end of each month (or all the ants there are if the ant population at that time is less than 7,000).
When the population of ants on the island exceeds the maximum of 28,000, feeding problems begin to occur, which reduces the growth rate to 31% per month.
Write the function count_ants that receives as parameters the number of ants that are at a given time on the island and a number of months. The function must calculate how many ants there will be on the island after that number of months.
Assume that population sampling is done only at the end of the month and that the anteater only eats at the end of the month.
This is my code:
def count_ants(current_ants, months):
growth_rate = 0.4 # Monthly growth rate of 40%
reduced_growth_rate = 0.31 # Reduced monthly growth rate of 31%
maximum_capacity = 28000 # Maximum capacity of the island
anteater = 7000 # Number of ants the anteater eats at the end of each month
for _ in range(months):
if current_ants > 0:
if current_ants <= maximum_capacity:
current_ants += int(current_ants * growth_rate)
current_ants -= min(current_ants, anteater)
else:
current_ants += int(current_ants * reduced_growth_rate)
current_ants -= min(current_ants, anteater)
return current_ants
My code works for the following cases:
- Correctly calculate cases with more than 7,000 ants at the end of the month
- Correctly calculates cases with more than 7000 ants in several months
- Correctly calculates cases with less than 7000 ants and one month
- Correctly calculates cases with less than 7000 ants and two months
- Correctly calculate cases without ants
- Correctly calculate cases with more than 28,000 ants at the end of the month
But it seems to fail in this one :
NO Correctly calculates cases with more than 28,000 ants in several months Your program failed when these inputs were used: initial_quantity: 28000 months: 4 Your program responded: 44205
I want to know why this particular test case failed. Thanks!
28000- which is the maximum capacity, maybe check forcurrent_ants < maximum_capacityinstead ofcurrent_ants <= maximum_capacity?current_ants < maximum_capacity