Reputation: 759
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
)
Upvotes: 3
Views: 12971
Reputation: 11
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
Upvotes: 0
Reputation: 1
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
Upvotes: 0
Reputation: 266
Easiest way to reverse a string
s = "chetan barawkar"
b = s.length - 1
while b >= 0
print s[b]
b=b-1
end
Upvotes: 6
Reputation: 51
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))
Upvotes: 0
Reputation: 1071
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
.
Upvotes: 7
Reputation: 129
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
Upvotes: 0
Reputation: 6076
You're on the right track converting it to an array.
def reverse(str)
str.chars.sort_by.with_index { |_, i| -i }.join
end
Upvotes: 2
Reputation: 27207
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.
Upvotes: 2