Reverse Groups (CodeEval) in python2