reverse and replace hackerrank solution in python