Python implementation of Markov algorithm