Reverse a string without using reversed() or [::-1]?

前端 未结 30 2533
南旧
南旧 2020-11-30 19:44

I came across a strange Codecademy exercise that required a function that would take a string as input and return it in reverse order. The only problem was you could not use

30条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-30 20:16

    Not very clever, but tricky solution

    def reverse(t):
        for j in range(len(t) // 2):
            t = t[:j] + t[- j - 1] + t[j + 1:- j - 1] + t[j] + t[len(t) - j:]
        return t
    

提交回复
热议问题