def swap(aList):
if len(aList) == 0:
return 0
elif len(aList) == 1:
print(aList[0])
return aList[0]
return aList[0] + swap(aList[2:])
aList = [
Why use a 2D array? You are just swapping its members (1D arrays) rather than the characters in your string. Just pass in the string itself - the indexing operator can access each character. Also, remember that the +
operator is non-commutative for strings:
def swap(s):
if len(s) == 0:
return ""
elif len(s) == 1:
return s
return s[1] + s[0] + swap(s[2:])
print(swap("abcdefgh")) # --> badcfehg