7/15 Look! I did it!


This was driving me nuts, I had to share! :smiley:

Here's my solution, which IS accepted finally:

def reverse(text):
    result = []
    step = 1
    while step < (len(text) + 1):
        result.append(text[len(text) - step])
        step += 1
    return "".join(result)

This was interesting to me because it doesn't use range() to decrement through the list, which a lot of other people have posted as a solution, but the question seems to disallow when it says "You may not use reversed or [::-1] to help you with this." (I'm not sure what that means if that's not it.) Also, it seems to be pretty compact. Not the most compact solution I've seen here in the QA, but I'm trying at every case to insert the calculation of a value instead of like doing an assignment statement with the calculation to some other variable, then using the new variable as the return, etc. (Is this a good thing?)

Also it was interesting to me that I actually produced a decrementing step through the index of the string by starting with a count variable step, starting with step = 1 , then stepping using len(text) - step. So, assuming a 6-character string, the first iteration appends the value at index( length of string minus step (1), so index 5, then text(length of string minus step (2)), so 4, until we get to index length of string minus step (6), for a total of 0. And this has to be included because we don't want to leave out the first character!