I don't get it


#1



def reverse(text):
    x = ""
    for i in range(len(text)):
        x = x + text[len(text)-i-1]
    return x

How does this make the text print ones self backwards?


#2

Pencil it through...

Say that text is 7 characters in length.

7 - 0 - 1 == 6
7 - 1 - 1 == 5
7 - 2 - 1 == 4
7 - 3 - 1 == 3
7 - 4 - 1 == 2
7 - 5 - 1 == 1
7 - 6 - 1 == 0

See how the indices are reversed?


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.