From ba0b8d81f729d15758bbb11911ce91fba3e831d7 Mon Sep 17 00:00:00 2001 From: Kristy Hisaw Date: Tue, 24 Sep 2019 16:17:31 -0700 Subject: [PATCH 1/2] methods added --- lib/reverse_sentence.rb | 28 +++++++- lib/sort_by_length.rb | 20 +++++- test/reverse_sentence_test.rb | 68 ++++++++++--------- ...rt_by_length.rb => sort_by_length_test.rb} | 4 +- 4 files changed, 79 insertions(+), 41 deletions(-) rename test/{sort_by_length.rb => sort_by_length_test.rb} (99%) diff --git a/lib/reverse_sentence.rb b/lib/reverse_sentence.rb index 3fe7cac..8acc832 100644 --- a/lib/reverse_sentence.rb +++ b/lib/reverse_sentence.rb @@ -1,6 +1,28 @@ # A method to reverse the words in a sentence, in place. -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(1) + def reverse_sentence(my_sentence) - raise NotImplementedError + + if my_sentence == "" + return "" + elsif my_sentence == nil + return nil + end + + reverse = my_sentence.scan(/(\s*)(\w*'*\w+[!,.?]*)(\s*)/).flatten + i = 0 + j = reverse.length - 1 + + (reverse.length / 2).times do + temp = reverse[i] + reverse[i] = reverse[j] + reverse[j] = temp + i += 1 + j -= 1 + end + my_sentence = reverse.join + return my_sentence + + end diff --git a/lib/sort_by_length.rb b/lib/sort_by_length.rb index a5713ad..c8b6d12 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) +# Space complexity: O(n) def sort_by_length(my_sentence) - raise NotImplementedError, "Method not implemented" + my_array = my_sentence.split(" ") + i = 1 + + while i < my_array.length + to_insert = my_array[i] + j = i + + while j > 0 && my_array[j-1].length > to_insert.length + my_array[j] = my_array[j-1] + j -= 1 + end + my_array[j] = to_insert + i += 1 + end + return my_array end diff --git a/test/reverse_sentence_test.rb b/test/reverse_sentence_test.rb index 346069b..534aaf2 100644 --- a/test/reverse_sentence_test.rb +++ b/test/reverse_sentence_test.rb @@ -1,74 +1,76 @@ require_relative "test_helper" +require 'pry' describe "reverse sentence" do describe "basic tests" do it "reverse a sentence with two words" do test_string = "hello, world" - + reverse_sentence(test_string) - + test_string.must_equal "world hello," end - + it "reverse a sentence with three words" do test_string = "Yoda is awesome!" - + reverse_sentence(test_string) - + test_string.must_equal "awesome! is Yoda" end end - + # check for edge cases describe "edge cases" do # if it's a string parameter, check for empty it "reverse an empty sentence" do test_string = "" - + reverse_sentence(test_string) - + test_string.must_be_empty end - + # if the parameter is an object, check for nil it "nil object passed to sentence reverse" do test_string = nil - + reverse_sentence(test_string) - + test_string.must_be_nil end - + it "reverse a sentence with one word" do test_string = "world" - + reverse_sentence(test_string) - + test_string.must_equal "world" end - + it "reverse a sentence with multiple words" do test_string = "I'm a better engineer today than I was yesterday." - + reverse_sentence(test_string) - + test_string.must_equal "yesterday. was I than today engineer better a I'm" end - + it "reverse a sentence with multiple spaces between words" do test_string = "How do you like them apples?" - - reverse_sentence(test_string) - - test_string.must_equal "apples? them like you do How" - end - - it "reverse a sentence with preceeding and trailing white spaces" do - test_string = " I can do this! " - - reverse_sentence(test_string) - - test_string.must_equal " this! do can I " - end - end -end + + reverse_sentence(test_string) + + test_string.must_equal "apples? them like you do How" + end + + it "reverse a sentence with preceeding and trailing white spaces" do + test_string = " I can do this! " + + reverse_sentence(test_string) + + test_string.must_equal " this! do can I " + end + end + end + \ No newline at end of file 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..1eb292f 100644 --- a/test/sort_by_length.rb +++ b/test/sort_by_length_test.rb @@ -8,11 +8,11 @@ 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 From e20c30a995415b099b61610062c4d80debc8f7fb Mon Sep 17 00:00:00 2001 From: Kristy Hisaw Date: Wed, 25 Sep 2019 16:10:36 -0700 Subject: [PATCH 2/2] updated comments, unable to get test to pass for string reversal --- lib/reverse_sentence.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/reverse_sentence.rb b/lib/reverse_sentence.rb index 8acc832..1d8df13 100644 --- a/lib/reverse_sentence.rb +++ b/lib/reverse_sentence.rb @@ -9,7 +9,7 @@ def reverse_sentence(my_sentence) elsif my_sentence == nil return nil end - + # regex credit goes to Amal reverse = my_sentence.scan(/(\s*)(\w*'*\w+[!,.?]*)(\s*)/).flatten i = 0 j = reverse.length - 1