string algrithm with python