Home > katas > Reversed Strings (8kyu) [Python]

Reversed Strings (8kyu) [Python]

Chek kata on Codewars


Complete the solution so that it reverses the string passed into it.

'world'  =>  'dlrow'

Solution 1

Let's start with loop solutions.

def solution(str):
    answer = ''
    for char in str:
        answer = char + answer
    return answer

Solution 2

Let's solve it with reverse & join.

def solution(str):
    answer = list(str)
    return ''.join(answer)

def solution(str):
    return ''.join(reversed(str))

Solution 3

Let's solve it with reduce.

# add import => from functools import reduce

def solution(str):
    return reduce(lambda acc, cur: cur + acc, str)

Solution 4

Let's solve it with slicing.

def solution(str):
    return str[::-1]

Solution 5

Let's solve it with recursion.

def solution(str):
    return solution(str[1:]) + str[0] if len(str) != 0 else str
© 2021, Andrew Losseff