Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

passing tests #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 60 additions & 3 deletions lib/reverse_sentence.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,61 @@
# A method to reverse the words in a sentence, in place.
def reverse_sentence(my_sentence)
raise NotImplementedError
require 'pry'

def word_reverse(my_string, right_index, left_index)

return if (my_string == nil || my_string.length == 0 )
return my_string if (left_index == right_index)

right = right_index
left = left_index
while right < left
left_container = my_string[left]
my_string[left] = my_string[right]
my_string[right] = left_container
right += 1
left -= 1
end
return my_string
end

def reverse_words(my_words)

return if (my_words == nil || my_words.length == 0)

right_index = 0
left_index = 0

while left_index < my_words.length

while my_words[right_index] == ' ' && right_index < my_words.length
right_index += 1
end

left_index = right_index + 1

while my_words[left_index]!= ' ' && left_index < my_words.length
left_index += 1
end

left_index -= 1

word_reverse(my_words, right_index, left_index)

right_index = left_index + 1
end

return my_words

end

def reverse_sentence(sentence)

return if sentence == nil || sentence.length < 2

right_index = 0
left_index = sentence.length - 1

word_reverse(sentence, right_index, left_index)

reverse_words(sentence)

end