def reverse(x):
if len(x) <= 1: #Check if the string argument given is more than one letter
return x #if it is return the string
return reverse(x[1:]) + x[0] #if it is not reverse the string using list indexing
Note: This is not a solution on how to do it but a reference to give you an idea of alternative ways to achieve this task.
I like that you are just giving alternative ways and additional information.
def reverse(text):
reverse_string = "" #creates empty string to add onto
for i in range(len(text)): #numbers 1 through however long the text is
reverse_string += text[len(text)-i-1] #it adds each letter in text to the reverse_string backwards
return reverse_string
This is one thatโs not quite as slim, but shows some simple, elegant practices.
edit: maybe not elegant, but still pretty easy.
Can you please explain what actually happens in the list indexing line in your code. It is some recursive loop because it starts with x[1] and goes till end of the x in reverse function. But i donโt get how is it reversing the string.
Sorry to have to do this but this topic has gone outside of the expected norm for this course module. Recusion has not been taught and should not be demonstrated in the track category. It should be discussed in the Corner Bar, by rights.