Reverse a String in Ruby

Reversing a Ruby String, without .reverse method

You need to stop the search for alternative or clever methods, such as altering things so you can .sort them. It is over-thinking the problem, or in some ways avoiding thinking about the core problem you have been asked to solve.

What this test is trying to get you you to do, is understand the internals of a String, and maybe get an appreciation of how String#reverse might be implemented using the most basic string operations.

One of the most basic String operations is to get a specific character from the string. You can get the first character by calling string[0], and in general you can get the nth character (zero-indexed) by calling string[n].

In addition you can combine or build longer strings by adding them together, e.g. if you had a="hell" and b="o", then c = a + b would store "hello" in the variable c.

Using this knowledge, find a way to loop through the original string and use that to build the reverse string, one character at a time. You may also need to look up how to get the length of a string (another basic string method, which you will find in any language's string library), and how to loop through numbers in sequence.

Reverse a String in Ruby by Reading Backwards

There are a few issue here but the bottom line is that you're looping wrong. You can either count up to a number or down from a number and it looks like you want to count down by starting at word.length. That's fine, but let's look and see what you're actually doing.

With while i > word.length || i != -1 you're checking each iteration that i is...greater than the length of the word? How would it get that way (you're not adding to i anywhere) and why would you want to check that?

Since you chose to count down, we want to stop when there are no letters remaining. So change your condition to while i > 0. Now we will loop only while there are letters left to go through.

There's another problem though - because indices start at 0, trying to get word[i] when i == 3 will get you nil! So you actually want to move the i = i - 1 to be the first line within your loop.

After these changes, you should have:

def is_reversed(word)
i = word.length
reverse = ""
while i > 0
i = i - 1
letter = word[i]
reverse += letter
end
return reverse
end

puts is_reversed("cat")

Reverse words of a string in Ruby?

The gist of any reverse operation is to iterate over elements in the reverse order of what you'd normally do. That is, where you'd usually use the set (0..N-1) you'd instead go through (N-1..0) or more specifically N-1-i where i is 0..N-1:

class String
def reverse_words
split(/\s+/).map{|w|wl=w.length-1;(0..wl).map{|i|w[wl-i]}.join}.join(' ')
end
end

puts "this is reverse test".reverse_words.inspect
# => "siht si esrever tset"

The same principle can be applied to the words in a given string.

Interview questions of this sort are of highly dubious value. Being "clever" in production code is usually a Very Bad Idea.



Related Topics



Leave a reply



Submit