diff --git a/lib/reverse_sentence.rb b/lib/reverse_sentence.rb index 3fe7cac..e8a424c 100644 --- a/lib/reverse_sentence.rb +++ b/lib/reverse_sentence.rb @@ -1,6 +1,31 @@ +#require "pry" # 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 + #raise NotImplementedError + if my_sentence == nil + return my_sentence + end + words = my_sentence.split() + i = 0 + j = words.length - 1 + + while i < j do + temp = words[i] + # binding.pry + words[i] = words[j] + words[j] = temp + # binding.pry + i += 1 + j -= 1 + end + # binding.pry + # print words + final_word = words.join(" ") + return final_word end + + + + diff --git a/lib/sort_by_length.rb b/lib/sort_by_length.rb index a5713ad..2380dfd 100644 --- a/lib/sort_by_length.rb +++ b/lib/sort_by_length.rb @@ -1,7 +1,19 @@ # 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) - raise NotImplementedError, "Method not implemented" + #raise NotImplementedError, "Method not implemented" + new_sentence = my_sentence.split() + n = new_sentence.length + for j in 0..n-1 + for i in 0..n-2-j + if new_sentence[i].length > new_sentence[i + 1].length + temp = new_sentence[i] + new_sentence[i] = new_sentence[i+1] + new_sentence[i + 1] = temp + end + end + end + return new_sentence end diff --git a/test/reverse_sentence_test.rb b/test/reverse_sentence_test.rb index 346069b..49c5d79 100644 --- a/test/reverse_sentence_test.rb +++ b/test/reverse_sentence_test.rb @@ -5,70 +5,78 @@ it "reverse a sentence with two words" do test_string = "hello, world" - reverse_sentence(test_string) + # reverse_sentence(test_string) + reverse_sentence(test_string).must_equal "world hello," - test_string.must_equal "world hello," + # test_string.must_equal "world hello," end it "reverse a sentence with three words" do test_string = "Yoda is awesome!" - reverse_sentence(test_string) + #reverse_sentence(test_string) - test_string.must_equal "awesome! is Yoda" + #test_string.must_equal "awesome! is Yoda" + reverse_sentence(test_string).must_equal "awesome! is Yoda" end end - # check for edge cases + #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 + #reverse_sentence(test_string) + #test_string.must_be_empty + reverse_sentence(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) + #reverse_sentence(test_string) - test_string.must_be_nil + #test_string.must_be_nil + reverse_sentence(test_string).must_be_nil + end it "reverse a sentence with one word" do test_string = "world" - reverse_sentence(test_string) + #reverse_sentence(test_string) - test_string.must_equal "world" + #test_string.must_equal "world" + reverse_sentence(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) + #reverse_sentence(test_string) - test_string.must_equal "yesterday. was I than today engineer better a I'm" + #test_string.must_equal "yesterday. was I than today engineer better a I'm" + reverse_sentence(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) + #reverse_sentence(test_string) - test_string.must_equal "apples? them like you do How" + #test_string.must_equal "apples? them like you do How" + reverse_sentence(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) + #reverse_sentence(test_string) - test_string.must_equal " this! do can I " + #test_string.must_equal " this! do can I " + reverse_sentence(test_string).must_equal " this! do can I " end end 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