Reverse using periodic and other methods


#1

One way to do it is by the periodic method,

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. :slight_smile:


15/7 Reverse Unnecessary code?
#2

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. :slight_smile:


#3

Clean and concise. :ok_hand:

Point: Basic UX, I would suggest including just one if condition to prevent a user from trying to reverse one letter. :sweat_smile:


#4

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.


#5

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.