Hello and welcome back, in this episode we are going to solve a python related problem in Codewars. Before we start I just want to say that this post is related to python programming, you are welcome to leave your comments below this post if and only if they are related to the below solution, kindly do not leave any comment which has nothing to do with python programming under this article, thank you.
In a small town, the population is
at p0 = 1000 at the beginning of a year. The population regularly increases
by 2 percent per year and moreover,
50 new inhabitants per year come to live in the town. How many years does the town need to see its population greater or equal to
p = 1200inhabitants? You need to round up the percentage part of the equation. Below is the entire solution to this question.
At the end of the first year there will be: 1000 + 1000 * 0.02 + 50 => 1070 inhabitants At the end of the 2nd year there will be: 1070 + 1070 * 0.02 + 50 => 1141 inhabitants (number of inhabitants is an integer) At the end of the 3rd year there will be: 1141 + 1141 * 0.02 + 50 => 1213 It will need 3 entire years.
So how are we going to turn the above population equation into a function?
def nb_year(p0, percent, aug, p): # p0 is the present total population, aug is the number of new inhabitants per year and p is the target population needs to be surpassed perc = round(p0 * percent/100) total_population = p0 + (perc) + aug year = 1 while(total_population < p): perc = round(total_population * percent/100) total_population = total_population + perc + aug year += 1 return year
Simple solution, hope you do enjoy this post. We will start a new project soon so stay tuned!