Given a string, return it reversed.

1
"foobar" ⟹ "raboof"

Solution 1

1
2
3
4
5
6
7
def myreversed(s):
    result = ''
    slen = len(s) - 1
    while slen >= 0:
        result += s[slen]
        slen -= 1
    return result

Solution 2

1
2
def myreversed(s):
    return s if not s else myreversed(s[1:]) + s[0]

Tests

1
2
3
4
5
import pytest
from reverse import myreversed

def test_myreversed():
    return myreversed("foobar") == "raboof"