From b189ba4c821cfb08690930575027b877294aeec9 Mon Sep 17 00:00:00 2001 From: Jessie Zhang Date: Fri, 28 Sep 2018 09:02:58 -0700 Subject: [PATCH] reverse_sentence --- lib/reverse_sentence.rb | 51 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/lib/reverse_sentence.rb b/lib/reverse_sentence.rb index af2a20b..16ae0f0 100644 --- a/lib/reverse_sentence.rb +++ b/lib/reverse_sentence.rb @@ -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