From 0c067c5d69e3a83813cfbaf6c0adf1da2f94e796 Mon Sep 17 00:00:00 2001 From: Madaleine Shields Date: Sat, 29 Sep 2018 08:31:22 -0700 Subject: [PATCH 1/4] Write reverse_words with helper method --- lib/reverse_words.rb | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/lib/reverse_words.rb b/lib/reverse_words.rb index cb3cc8a..03ba5d1 100644 --- a/lib/reverse_words.rb +++ b/lib/reverse_words.rb @@ -1,4 +1,44 @@ # A method to reverse each word in a sentence, in place. +def reverse_word(string, beginning, ending) + + i = beginning + j = ending + + while j > i + temp = string[i] + string[i] = string[j] + string[j] = temp + i += 1 + j -= 1 + end +end + def reverse_words(my_words) - raise NotImplementedError + + if my_words == nil || my_words.length < 2 + return my_words + end + + i = 0 + j = 0 + + while j < my_words.length + + i = j + + while my_words[j] == " " + i += 1 + j += 1 + end + + while my_words[j] != " " && my_words[j] != nil + j += 1 + end + + reverse_word(my_words, i, j-1) + + j += 1 + end + + return my_words end From fad50d4c949b0e44d98feb54aa55f04a0a3b022d Mon Sep 17 00:00:00 2001 From: Madaleine Shields Date: Sat, 29 Sep 2018 08:34:39 -0700 Subject: [PATCH 2/4] change helper method name and var names --- lib/reverse_words.rb | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/reverse_words.rb b/lib/reverse_words.rb index 03ba5d1..cb1a0f1 100644 --- a/lib/reverse_words.rb +++ b/lib/reverse_words.rb @@ -1,15 +1,17 @@ # A method to reverse each word in a sentence, in place. -def reverse_word(string, beginning, ending) +def string_reverse(my_string, beginning, ending) + front_index = beginning + back_index = ending - i = beginning - j = ending + while front_index < back_index + front_value = my_string[front_index] + back_value = my_string[back_index] - while j > i - temp = string[i] - string[i] = string[j] - string[j] = temp - i += 1 - j -= 1 + my_string[front_index] = back_value + my_string[back_index] = front_value + + front_index += 1 + back_index -= 1 end end @@ -35,7 +37,7 @@ def reverse_words(my_words) j += 1 end - reverse_word(my_words, i, j-1) + string_reverse(my_words, i, j-1) j += 1 end From 401ccd156f83edd744d91cfb02925d403c150c3c Mon Sep 17 00:00:00 2001 From: Madaleine Shields Date: Sat, 29 Sep 2018 08:48:08 -0700 Subject: [PATCH 3/4] Refactor --- lib/reverse_words.rb | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/lib/reverse_words.rb b/lib/reverse_words.rb index cb1a0f1..a6ef3b8 100644 --- a/lib/reverse_words.rb +++ b/lib/reverse_words.rb @@ -16,31 +16,25 @@ def string_reverse(my_string, beginning, ending) end def reverse_words(my_words) - - if my_words == nil || my_words.length < 2 - return my_words - end + return if my_words == nil i = 0 - j = 0 - - while j < my_words.length - - i = j + length = my_words.length - while my_words[j] == " " + while i < length + while my_words[i] == ' ' && i < length i += 1 - j += 1 end - while my_words[j] != " " && my_words[j] != nil - j += 1 + beginning = i + + while my_words[i] != ' ' && i < length + i += 1 end - string_reverse(my_words, i, j-1) + ending = i - 1 - j += 1 + string_reverse(my_words, beginning, ending) end - return my_words end From 116a82c63b49fb9846d4ac9b653890b6db2cd158 Mon Sep 17 00:00:00 2001 From: Madaleine Shields Date: Sat, 29 Sep 2018 13:12:32 -0700 Subject: [PATCH 4/4] Add time/space complexities --- lib/reverse_words.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/reverse_words.rb b/lib/reverse_words.rb index a6ef3b8..350d1fe 100644 --- a/lib/reverse_words.rb +++ b/lib/reverse_words.rb @@ -38,3 +38,7 @@ def reverse_words(my_words) end return my_words end + +# time complexity: O(n) - goes through the loop n times, in this case it is through the length of my_words + +# space complexity: O(1) - the storage doesn’t grow with the size of the input, stays constant with the length of the string