From 4307b6e303c67734da3890189f276ebde25fff6a Mon Sep 17 00:00:00 2001 From: Tiffany Chio Date: Thu, 19 Sep 2019 11:55:24 -0700 Subject: [PATCH 1/3] sorting sentences complete --- lib/sort_by_length.rb | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/sort_by_length.rb b/lib/sort_by_length.rb index a5713ad..a5a49a2 100644 --- a/lib/sort_by_length.rb +++ b/lib/sort_by_length.rb @@ -3,5 +3,29 @@ # Time complexity: ? # Space complexity: ? def sort_by_length(my_sentence) - raise NotImplementedError, "Method not implemented" + + # I'm assuming split is O(n), + # can't imagine it needing more than one pass through n + sent_array = my_sentence.split(" ") + count = sent_array.length + + if count == 0 + return sent_array + end + + i = 1 + while i < count + to_insert = sent_array[i] + j = i + + while j > 0 && sent_array[j-1].length > to_insert.length + sent_array[j] = sent_array[j-1] + j -= 1 + end + + sent_array[j] = to_insert + i += 1 + end + + return sent_array end From 455ea2068dd548c543cdb9dd3134dc378e020f9b Mon Sep 17 00:00:00 2001 From: Tiffany Chio Date: Sun, 29 Sep 2019 21:14:58 -0700 Subject: [PATCH 2/3] working version of both exercises. need to optimize reverse sentence --- lib/reverse_sentence.rb | 30 +++++++++++++++++-- lib/sort_by_length.rb | 4 +-- ...rt_by_length.rb => sort_by_length_test.rb} | 6 ++-- 3 files changed, 32 insertions(+), 8 deletions(-) rename test/{sort_by_length.rb => sort_by_length_test.rb} (98%) diff --git a/lib/reverse_sentence.rb b/lib/reverse_sentence.rb index 3fe7cac..f6df395 100644 --- a/lib/reverse_sentence.rb +++ b/lib/reverse_sentence.rb @@ -1,6 +1,30 @@ # A method to reverse the words in a sentence, in place. -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n^2) +# Space complexity: O(1) def reverse_sentence(my_sentence) - raise NotImplementedError + return nil if my_sentence == nil + + len = my_sentence.length + + (len / 2).times do |i| + temp_end_of_sentence = my_sentence[len - i - 1] + my_sentence[len - i - 1] = my_sentence[i] + my_sentence[i] = temp_end_of_sentence + end + + first_index = 0 + + len.times do |j| + if my_sentence[j + 1] == " " || j == len - 1 + ((j - first_index + 1) / 2).times do |k| + temp_last_char = my_sentence[j - k] + my_sentence[j - k] = my_sentence[first_index + k] + my_sentence[first_index + k] = temp_last_char + end + + first_index = j + 2 + end + end + + return my_sentence end diff --git a/lib/sort_by_length.rb b/lib/sort_by_length.rb index a5a49a2..44c8785 100644 --- a/lib/sort_by_length.rb +++ b/lib/sort_by_length.rb @@ -1,7 +1,7 @@ # 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(1) def sort_by_length(my_sentence) # I'm assuming split is O(n), diff --git a/test/sort_by_length.rb b/test/sort_by_length_test.rb similarity index 98% rename from test/sort_by_length.rb rename to test/sort_by_length_test.rb index c38d976..626ecbe 100644 --- a/test/sort_by_length.rb +++ b/test/sort_by_length_test.rb @@ -8,12 +8,12 @@ it "will return an array of words, by length" do expect(sort_by_length("I love Ada")).must_equal ["I", "Ada", "love"] end - + 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("words of equal length")).must_equal ["of", "words", "equal", "length"] end - + 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 4752660aebf3ad9e2e745fa09ca2af95f6751ee6 Mon Sep 17 00:00:00 2001 From: Tiffany Chio Date: Sun, 29 Sep 2019 21:18:17 -0700 Subject: [PATCH 3/3] final version --- lib/reverse_sentence.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/reverse_sentence.rb b/lib/reverse_sentence.rb index f6df395..9ec2806 100644 --- a/lib/reverse_sentence.rb +++ b/lib/reverse_sentence.rb @@ -7,9 +7,9 @@ def reverse_sentence(my_sentence) len = my_sentence.length (len / 2).times do |i| - temp_end_of_sentence = my_sentence[len - i - 1] + temp = my_sentence[len - i - 1] my_sentence[len - i - 1] = my_sentence[i] - my_sentence[i] = temp_end_of_sentence + my_sentence[i] = temp end first_index = 0 @@ -17,9 +17,9 @@ def reverse_sentence(my_sentence) len.times do |j| if my_sentence[j + 1] == " " || j == len - 1 ((j - first_index + 1) / 2).times do |k| - temp_last_char = my_sentence[j - k] + temp = my_sentence[j - k] my_sentence[j - k] = my_sentence[first_index + k] - my_sentence[first_index + k] = temp_last_char + my_sentence[first_index + k] = temp end first_index = j + 2