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

reverse_sentence: jessie #24

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
51 changes: 50 additions & 1 deletion lib/reverse_sentence.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,53 @@
# A method to reverse the words in a sentence, in place.
require 'pry'

def partial_reverse(my_string, start_index, end_index)
i = start_index
j = end_index

while i < j
temp = my_string[i]
my_string[i] = my_string[j]
my_string[j] = temp
i += 1
j -= 1
end
return
end

def reverse_words(my_words)
return if my_words.nil? || my_words.length == 0

i=0
length = my_words.length
while i < length
while my_words[i] == " " && i < length
i += 1
end

start_index = i

while my_words[i] != " " && i < length
i += 1
end

end_index = i-1

partial_reverse(my_words, start_index, end_index)
end

return
end

def reverse_sentence(my_sentence)
raise NotImplementedError
return if my_sentence.nil? || my_sentence.length == 0

i = 0
length = my_sentence.length
j = length - 1

partial_reverse(my_sentence, i, j)
reverse_words(my_sentence)

return my_sentence
end