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

Reversed Strings (8kyu) [Ruby]


Chek kata on Codewars

Description:

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 = []
    chars = str.split("")
    for char in chars
        answer.unshift(char)
    end
    return answer.join("")
end

Solution 2

Let's solve it with reverse.

def solution(str)
    return str.split("").reverse().join("")
end

def solution(str)
    return str.reverse
end

Solution 3

Let's solve it with reduce.

def solution(str)
    return str.split("").reduce("") {|acc, cur| cur + acc}
end
© 2021, Andrew Losseff