Recursively reverse a string by Python

# During reversion, first char in string will become last one. Then if we have reversed version of the string (from 1 to last), then we know the result ! That is reverse(s[1:]) + s[0]. Base case is when it is empty string, in which case we simply return empty string.

def reverse(s):
if not s: return s
return reverse(s[1:])+s[0]

Comments

Popular posts from this blog

Check MySQL query history from command line

Installing chocolatey on Windows

Fixed: ImportError: No module named utilities.common.tools