3

I am working on this coding challenge, and I have found that I am stuck. I thought it was possible to call the .string method on an argument that was passed in, but now I'm not sure. Everything I've found in the Ruby documentation suggests otherwise. I'd really like to figure this out without looking at the solution. Can someone help give me a push in the right direction?

# Write a method that will take a string as input, and return a new
# string with the same letters in reverse order.
# Don't use String's reverse method; that would be too simple.
# Difficulty: easy.

def reverse(string)
string_array = []

string.split()

string_array.push(string)

string_array.sort! { |x,y| y <=> x}
end

# These are tests to check that your code is working. After writing
# your solution, they should all print true.

puts(
  'reverse("abc") == "cba": ' + (reverse("abc") == "cba").to_s
)
puts(
  'reverse("a") == "a": ' + (reverse("a") == "a").to_s
)
puts(
  'reverse("") == "": ' + (reverse("") == "").to_s
)
2
  • What is the .string method? I've never heard of it. There's .to_s which might be what you're thinking of. In any case, what you're doing here is a bit nuts and missing the point. Hint: Try iterating over each character in one string, and putting those characters into another with an inverted offset. Character N goes to 0, N-1 goes to 1, etc. Commented Jul 1, 2015 at 20:57
  • You may wish to look at the answers here. Commented Jul 2, 2015 at 1:17

8 Answers 8

7

This is the simplest one line solution, for reversing a string without using #reverse, that I have come across -

"string".chars.reduce { |x, y| y + x }          # => "gnirts"

Additionally, I have never heard of the #string method, I think you might try #to_s.

Sign up to request clarification or add additional context in comments.

1 Comment

Could you please explain this code?
6

Easiest way to reverse a string

s = "chetan barawkar"

b = s.length - 1

while b >= 0

  print  s[b]

  b=b-1

end

Comments

3

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.

1 Comment

Thank you for the feedback. I've been working through the tutorials on codecademy and teamtreehouse.com. But I'm having a little trouble transitioning from the tutorials to real world problem solving.
2

You're on the right track converting it to an array.

def reverse(str)
  str.chars.sort_by.with_index { |_, i| -i }.join
end

Comments

0

Here is a solution I used to reverse a string without using .reverse method :

@string = "abcde"
@l = @string.length
@string_reversed = ""
i = @l-1
while i >=0 do
 @string_reversed << @string[i]
 i = i-1
end
return @string_reversed

Comments

0

Lol, I am going through the same challenge. It may not be the elegant solution, but it works and easy to understand:

puts("Write is a string that you want to print in reverse")
#taking a string from the user
string = gets.to_s #getting input and converting into string
def reverse(string)
  i = 0
  abc = [] # creating empty array
  while i < string.length
    abc.unshift(string[i]) #populating empty array in reverse
    i = i + 1
end
return abc.join   
end
puts ("In reverse:  " + reverse(string))

Comments

0

Thought i'd contribute my rookie version.

def string_reverse(string)
  new_array = []
  formatted_string = string.chars
  new_array << formatted_string.pop until formatted_string.empty?
  new_array.join
end

Comments

0
def reverse_str(string)
  # split a string to create an array
  string_arr = string.split('')
  
  result_arr = []
  i = string_arr.length - 1

  # run the loop in reverse
  while i >=0
    result_arr.push(string_arr[i])
    i -= 1
  end

  # join the reverse array and return as a string
  result_arr.join
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.