From e3c9a8538f9612ee811d387d5e2d4794cff0a4b4 Mon Sep 17 00:00:00 2001 From: Paige Davenport Date: Mon, 23 Sep 2019 11:55:46 -0700 Subject: [PATCH 1/2] Changed name of sort_by_length test --- test/{sort_by_length.rb => sort_by_length_test.rb} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename test/{sort_by_length.rb => sort_by_length_test.rb} (99%) diff --git a/test/sort_by_length.rb b/test/sort_by_length_test.rb similarity index 99% rename from test/sort_by_length.rb rename to test/sort_by_length_test.rb index c38d976..e5103c1 100644 --- a/test/sort_by_length.rb +++ b/test/sort_by_length_test.rb @@ -16,4 +16,4 @@ it "will return an array of words by length, words that are of equal length will appear in the order they appear" do expect(sort_by_length("I love great awesome words")).must_equal ["I", "love", "great", "words", "awesome"] end -end \ No newline at end of file +end From 5051da4575ee1250aa43a566847c900c69303cce Mon Sep 17 00:00:00 2001 From: Paige Davenport Date: Mon, 23 Sep 2019 11:56:18 -0700 Subject: [PATCH 2/2] Working code for both methods. --- lib/reverse_sentence.rb | 72 +++++++++++++++++++++++++++++++++++++++-- lib/sort_by_length.rb | 20 ++++++++++-- 2 files changed, 86 insertions(+), 6 deletions(-) diff --git a/lib/reverse_sentence.rb b/lib/reverse_sentence.rb index 3fe7cac..054a73a 100644 --- a/lib/reverse_sentence.rb +++ b/lib/reverse_sentence.rb @@ -1,6 +1,72 @@ # A method to reverse the words in a sentence, in place. -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) - my method does not have any nested loops, but it will run through the length of the original string multiple times +# Space complexity: O(n) - a second variable (reverse) needs to be created, which will take up as much space as the original string def reverse_sentence(my_sentence) - raise NotImplementedError + if my_sentence != nil + # Creates array of words and spaces + words_array = words(my_sentence) + + # Revereses the order of the words + length = words_array.length + i = 0 + j = length - 1 + (length / 2).times do + # selects word at smallest index that has not been swapped + word_1 = words_array[i] + # selects words at largest index that has not been swapped + word_2 = words_array[j] + + words_array[i] = word_2 + words_array[j] = word_1 + + i += 1 + j -= 1 + end + + reverse = words_array.join + + # Overrides original string with order of characters in reverse + i = 0 + my_sentence.length.times do + my_sentence[i] = reverse[i] + i += 1 + end + end + + return my_sentence +end + +# Helper method to convert sentence into array of words - O(n) time complexity +def words(my_sentence) + length = my_sentence.length + words_array = [] + i = 0 + word = "" + + until i == length + l = my_sentence[i] + j = my_sentence[i + 1] + + if l != " " && j != " " + word += l + elsif l != " " && j == " " + word += l + words_array << word + word = "" + elsif l == " " && j == " " + word += l + elsif l == " " && j != " " + word += l + words_array << word + word = "" + end + + if i == length - 1 + words_array << word + end + + i += 1 + end + + return words_array end diff --git a/lib/sort_by_length.rb b/lib/sort_by_length.rb index a5713ad..5017566 100644 --- a/lib/sort_by_length.rb +++ b/lib/sort_by_length.rb @@ -1,7 +1,21 @@ # 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) +# Space complexity: O(n) def sort_by_length(my_sentence) - raise NotImplementedError, "Method not implemented" + new_sentence = my_sentence.split(" ") + length = new_sentence.length + i = 0 + + (length - 1).times do + j = new_sentence[i] + if new_sentence[i].length > new_sentence[i + 1].length + new_sentence[i] = new_sentence[i + 1] + new_sentence[i + 1] = j + end + + i += 1 + end + + return new_sentence end