Adding my Python code for the Head-Tail algorithm.