(Slightly) Non-Trivial Implementation of CLI Hangman. A Hands-on Python Tutorial.

Bored of learning python the regular way? Make a command line interface Hangman game and learn while you’re at it.

First try out the game for yourself! Run the following in your terminal,

pip install hangman-ultimate

Never played the chalk board game of Hangman before? Rules are simple, you have to guess the name of a random animal letter by letter. Every wrong guess of a letter will bring the man closer to hanging. In retrospection, the game is a little dark for me to have played as a kid.

Alternatively, you can download the code from here.

Remember to like this post and start my Repository on GitHub if this post helped you!

Tear apart the code and make sure you have learnt the following at the end of it,
1. Opening a file and reading from it line by line.
2. Making multi-line strings.
3. Python Classes and Class instances(objects).
4. Class fields(variables) and methods(functions).
5. Passing class objects to functions.
6. Shared and non-shared fields(variables) amongst objects of same class.
7. Calling system(terminal) commands via the python script.
8. Catching an error using try except block.
9. Making basic command line animations.
10. Making programs compatible with both python 2 and python 3.
11. Pip packaging your python programs!

Got more ideas for CLI based games? Put them in the comments below!

Custom ASCII art and random one-liner welcome messages on Linux Terminal start-up

Would it not be cool if your terminal welcomes you with an ASCII art and a random one-liner each time you start it up!

Like this:-

Custom welcome to terminal

Remember to like my post and star my GitHub repository if this post helps you!

Linux users (Sorry Mac owners (for the time being, use the custom method after this)) can simply install my poketerm pip package!

Remember to install with sudo permissions!

$ sudo pip install poketerm
$ poketerm -t 1
$ poketerm -h

You can customise the above according to your need or you can save yourself some trouble and use my code on github which does everything that I am going to describe below.

Firstly download and put this file in your /home/$USER/ folder.

All you need to do now is manipulate the .bashrc shell script that Bash(Terminal) runs whenever it is started interactively. You can put any command in that file that you could type at the command prompt and they will run whenever you start Bash.

Note: The following makes changes to default terminal Bash. If you are using some other shell you will have to make changes to their respective analogous file.

NOTE FOR MAC USERS: The .bashrc equivalent of Mac is .bash_profile, Mac users may use that file for the following changes.

After opening this file with your favourite editor with a command like

vim ~/.bashrc

You will find the line containing the following in the start:


Just after which you will have to add this small piece of code that I wrote:

echo "Welcome $USER "
echo "|\_                  _"
echo " \ \               _/_|"
echo "  \ \_          __/ /"
echo "   \  \________/   /"
echo "    |              |"
echo "    /              |"
echo "   |   0       0   |"
echo "   |       _       |"
echo "   |()    __    () |"
echo "    \    (__)      |"
if [ -f "$file" ]
    shuf -n 1 $file

The file should look similar to this now:

.bashrc code after addition of lines

Save it and you are good to go!

Most of the above code is self explanatory. The only thing that you might not get is the statement inside the if conditional. If the file exists, then a random line is returned out of the 10,000 lines in the fortunes.txt file.

What more can you do?

You can create your own custom ASCII art or you could find some here. Just format it and replace it with my noobish art. Do keep in mind to not use art which is very large in line length/height else it might not appear as intended. Also keep in mind the characters that are used in the art. Test the piece of code as a separate shell script before you add it to the file. You can also save the art in a file/folder and invoke one randomly just like the one-liner.

When is the Best Sorting Algorithm the best?

There are many a problems in the real world and to each problem, a solution. In computer science, this solution is referred to as an algorithm. However, to a single problem, there often are many a algorithms to achieve the required result. For finding the best solution, we do time complexity analysis(here on referred to as TCA).

For those of you who know the basics of TCA, would know that we select the algorithm which performs best for a very large input size. For example, given below is the graph depicting the run-time of two algorithms A and B  for increasing input sizes.


According to the conventional TCA, the algorithm A is better than the algorithm B, as for large input sizes the average time of execution is less for the algorithm A.

The algorithm A, as we can see, has a linear time complexity, O(n), where as its quadratic for algorithm B, O(n²). In general, in TCA we consider an O(n) algorithm better than a O(n²) algorithm, as for a very large input size (an input size tending to infinity actually), a O(n) algorithm will work better than a O(n²) algorithm.

Now consider the following example,example3

Here again according to TCA, the algorithm C is better than the algorithm D. However, notice that for input sizes less than 10,000 , the algorithm D performed better than the algorithm C.

Note that, an input size of 10,000 is quite big, and might be the actual input size of the problem. With the conventional TCA we would have used the algorithm C for our practical application, when it clearly should have been better to use the algorithm D.

A very common problem in computers, is that of sorting. That is given an input array of numbers, give an output array with increasing/decreasing order of the elements of the input array. For example,

Input: [7,3,8,4,2,5,1,6,9,0]

Output: [0,1,2,3,4,5,6,7,8,9]

There are as we know, quite a few solutions to this problem, you can check out these algorithms here.

Five most common sorting algorithms are

  1. Bubble Sort
  2. Insertion Sort
  3. Selection Sort
  4. Quick Sort
  5. Merge Sort

The time complexities of the above algorithms are as follows,


We know that quick sort is the best sorting algorithm among all the above five algorithms. For big input sizes, you may checkout this comparison.

However, the question that we are trying to address here, is that after what input size is quick sort the best, and which is the best sorting algorithm for input sizes less than that input size?

I wrote a python code which plots the variations of average time of execution (averaged for 1000 inputs of the same size) for various input sizes. I took the actual time of execution on my machine rather than the counts as this is exercise is for finding the most practical solution.


The reason for a lot of noise on in the graph are, the operating system overheads. It is to be noted that, I closed as many applications and background processes as I could. 

In spite of the noise, we still are able to make out that for a large input size what is the order or preference for sorting algorithms.

Quick > Merge > Insertion > Selection > Bubble

I ran multiple runs of the above code for small input sizes as well,

Here we notice that for input sizes <40 insertion sort performs the best out of all the above algorithms. So if you are working on some application which requires sorting of less than input sizes 40, you might want to consider using insertion sort rather than quick sort!

The code that I used for the above analysis can be found in the link,


Remember to like this post and start my GitHub repository if this post helped you!

Note that, you need to close as many applications as you can before the execution of the code. Once the code is running do not disturb the computer as it will raise additional overheads. If possible try to run the code while the system is offline.

There are still a lot of things that you can do with this code. Two such things are,

  1. Add more sorting algorithms for comparison from the plethora of sorting algorithms available here
  2. If you look carefully, you will see that merge sort performs horribly for small input sizes (probably attributed to the fact that it uses extra space for sorting). However, for big input sizes, it is the second best sorting algorithm. When does that happen? Try to find out!

Feel free to comment your opinions and pointing out errors. Thanks!