From 7416187a657a85bcc841a648a87e10530486c1e9 Mon Sep 17 00:00:00 2001 From: Maryam Shitu Date: Sun, 25 Nov 2018 16:04:30 -0800 Subject: [PATCH] all tests passing --- lib/reverse_sentence.rb | 50 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/lib/reverse_sentence.rb b/lib/reverse_sentence.rb index af2a20b..92c0a3c 100644 --- a/lib/reverse_sentence.rb +++ b/lib/reverse_sentence.rb @@ -1,4 +1,52 @@ # A method to reverse the words in a sentence, in place. def reverse_sentence(my_sentence) - raise NotImplementedError + def reverse_sentence(sentence) + return nil if sentence.nil? + + finish = nil + start = nil + on_first_word = true + + is_finding_whitespace = sentence[-1] == ' ' + + (sentence.length - 1).downto(0) do |current_index| + current_char = sentence[current_index] + + if is_finding_whitespace + if finish.nil? && current_char == ' ' + finish = current_index + end + + if start.nil? && sentence[current_index - 1] != ' ' + start = current_index + end + else + # a word + if finish.nil? && current_char != ' ' + finish = current_index + end + + if start.nil? && sentence[current_index - 1] == ' ' + start = current_index + end + end + + if start.nil? && current_index == 0 + start = current_index + end + + if start && finish + if on_first_word + on_first_word = false + else + word = sentence.slice!(start..finish) + sentence << word + end + + start = nil + finish = nil + is_finding_whitespace = !is_finding_whitespace + end + end +end end