From 48a56f5fdad3af8a23cc01082d44b1536e7ca3c6 Mon Sep 17 00:00:00 2001 From: Dominique Date: Mon, 30 Sep 2019 19:53:20 -0700 Subject: [PATCH] wave 2 complete --- lib/reverse_sentence.rb | 67 ++++++++++++++++++- lib/sort_by_length.rb | 27 +++++++- ...rt_by_length.rb => sort_by_length_test.rb} | 0 3 files changed, 88 insertions(+), 6 deletions(-) rename test/{sort_by_length.rb => sort_by_length_test.rb} (100%) diff --git a/lib/reverse_sentence.rb b/lib/reverse_sentence.rb index 3fe7cac..b47434f 100644 --- a/lib/reverse_sentence.rb +++ b/lib/reverse_sentence.rb @@ -1,6 +1,67 @@ + # A method to reverse the words in a sentence, in place. -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) because the algorithm becuase the time complexity will change as the size of the input changes +# Space complexity: O(1) because the same number of variables will be made regardless of input size def reverse_sentence(my_sentence) - raise NotImplementedError + + return my_sentence if my_sentence == nil + + counter = 0 + word_length = 0 + index = 0 + + word_start = 0 + # needs to be an outer loop that iterates thru the sentence + while counter < my_sentence.length + + word_length = 0 + + while my_sentence[counter] != " " && counter < my_sentence.length + # whatever index is when the loop exits, is where the word ends + word_length += 1 + counter += 1 + end + + word_end = counter - 1 + + while my_sentence[counter] == " " + counter += 1 + end + + # this is going to swap each letter in each word + word_reverse(my_sentence, word_start, word_end) + + word_start = counter + end + + i = 0 + + until i == my_sentence.length/2 + temp = my_sentence[i] + my_sentence[i] = my_sentence[my_sentence.length - i - 1] + my_sentence[my_sentence.length - i - 1] = temp + i += 1 + end + + return my_sentence + end + +def word_reverse(my_sentence, word_start, word_end) + word_length = word_end - word_start + 1 + + counter = 0 + + while counter != word_length/2 + temp = my_sentence[word_start] + my_sentence[word_start] = my_sentence[word_end] + my_sentence[word_end] = temp + + word_start += 1 + word_end -= 1 + counter += 1 + end +end + + + diff --git a/lib/sort_by_length.rb b/lib/sort_by_length.rb index a5713ad..0da371b 100644 --- a/lib/sort_by_length.rb +++ b/lib/sort_by_length.rb @@ -1,7 +1,28 @@ # A method which will return an array of the words in the string # sorted by the length of the word. -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n^2) because as the algorithm scales, each nested loop will run n number of times according to the input. +# Space complexity: O(1) because the same amount of memory will be used regardless of the input size. def sort_by_length(my_sentence) - raise NotImplementedError, "Method not implemented" + sentence_array = my_sentence.split + + i = 0 + swapped = true + + while i < sentence_array.length - 1 && swapped + j = 0 + swapped = false + while j < sentence_array.length - i - 1 + if sentence_array[j].length > sentence_array[j + 1].length + temp = sentence_array[j] + sentence_array[j] = sentence_array[j + 1] + sentence_array[j + 1] = temp + swapped = true + end + j += 1 + end + i += 1 + end + return sentence_array end + + diff --git a/test/sort_by_length.rb b/test/sort_by_length_test.rb similarity index 100% rename from test/sort_by_length.rb rename to test/sort_by_length_test.rb