Solution to moderate level CodeEval challenges