## codingdirectional: Growth of a Population

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 = 1200`inhabitants? 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!

Planet Python

## codingdirectional: Count the vowel characters within a string with Python function

Hello and welcome back, we will start a new python project soon but for now, let us continue solving some python related problems first in this post and beyond.

The problem we are going to solve in this article goes like this: Given a string consists of lower case characters and spaces, you need to write a function to count the total vowel characters within that string. The solution is as follow.

` def getCount(inputStr):     num_vowels = 0     li= list(inputStr)     vovel= ['a','e','i','o','u']     for i in li:         if i in vovel:             num_vowels+=1              return num_vowels `

The above function will only count the vowel character within a string and skips the other characters.

Planet Python

## codingdirectional: Reverse a number with Python

In this snippet, we are going to create a python method to reverse the order of a number. This is one of the questions on Codewars. If you enter -123 into the method you will get -321. If you enter 1000 into the method you will get 1. Below is the entire solution.

` def reverse_number(n):     num_list = list(str(n))     num_list.reverse()      if "-" in num_list:         num_list.pop(len(num_list)-1)         num_list.insert(0, "-")      return int("".join(num_list)) `

If you do follow my website you know that I always write simple python code and post them here, but starting from the next article, I will stop posting python code for a while and start to talk about my python journey and the cool software which is related to python. I hope you will appreciate this new style of writing and thus will make learning python for everyone a lot more fun than just staring at the boring and sometimes long python snippet.

If you have any solution for this problem do comment below.

Planet Python

## codingdirectional: Count the number of occurrences of each character and return it as a list of tuples in order of appearance

In this example we are going to create a function which will count the number of occurrences of each character and return it as a list of tuples in order of appearance. For example,

` ordered_count("abracadabra") == [('a', 5), ('b', 2), ('r', 2), ('c', 1), ('d', 1)] `

The above is a 7 kyu question on CodeWars, this is the only question I can solve today after the first two fail attempts.

I am supposed to start the Blender project today but because I want to write a post for your people I have spent nearly an hour and a half working on those three python questions on CodeWars, I hope you people will really appreciate my effort and will share this post to help this website to grown.

` def ordered_count(input):      already = []     input_list = list(input)     return_list = []     for word in input_list:         if(word not in already):             return_list.append((word, input_list.count(word)))             already.append(word)      return return_list `

The solution above is short and solid, hope you like it.

Planet Python

## codingdirectional: How to use the reverse method of a list in python

Most of the time a python programmer will need to reverse the order of the entire python list’s elements so a program can loop through those elements in the list starting from the end instead of from the beginning. Below is an example which we will use the python list’s reverse method to reverse the order of the list before using it.

Wolves have been reintroduced to Great Britain. You are a sheep farmer and are now plagued by wolves which pretend to be sheep. Fortunately, you are good at spotting them.

Warn the sheep in front of the wolf that it is about to be eaten. Remember that you are standing at the front of the queue which is at the end of the array:

` [sheep, sheep, sheep, sheep, sheep, wolf, sheep, sheep]      (YOU ARE HERE AT THE FRONT OF THE QUEUE)    7      6      5      4      3            2      1 `

If the wolf is the closest animal to you, return `"Pls go away and stop eating my sheep"`. Otherwise, return `"Oi! Sheep number N! You are about to be eaten by a wolf!"` where `N` is the sheep’s position in the queue.

Note: there will always be exactly one wolf in the array!

## Examples:

` warn_the_sheep(["sheep", "sheep", "sheep", "wolf", "sheep"]) == 'Oi! Sheep number 1! You are about to be eaten by a wolf!'  warn_the_sheep(['sheep', 'sheep', 'wolf']) == 'Pls go away and stop eating my sheep' `

Before we solve the above problem, we need to reverse the order of the list using the below method.

` queue.reverse() `

Here is the entire solution.

` def warn_the_sheep(queue):     queue.reverse()     the_wolf_position = queue.index("wolf")     if(the_wolf_position == 0):         return 'Pls go away and stop eating my sheep'     else:         return "Oi! Sheep number " + str(the_wolf_position) + "! You are about to be eaten by a wolf!" `

A simple solution indeed, in the meantime I feel really shocked that the animals (both the wolf and the sheep) in Great Britain knows English so well! Like, share or follow me on Twitter.

Planet Python