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

前端 未结 30 2581
南旧
南旧 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:00

    Here is one using a list as a stack:

    def reverse(s):
      rev = [_t for _t in s]
      t = ''
      while len(rev) != 0:
        t+=rev.pop()
      return t
    

提交回复
热议问题