diff --git a/lib/reverse_sentence.rb b/lib/reverse_sentence.rb index 3fe7cac..784338c 100644 --- a/lib/reverse_sentence.rb +++ b/lib/reverse_sentence.rb @@ -1,6 +1,87 @@ # A method to reverse the words in a sentence, in place. -# Time complexity: ? -# Space complexity: ? + +# Time complexity: O(n^2) Where n is the length of the string, my_sentence. We iterate through the entire sentence but when a "word" is reached we iterate through that word, swapping each letter. +# Space complexity: O(1) because nothing new is created, everything is swapped/reversed in place. + def reverse_sentence(my_sentence) - raise NotImplementedError + if my_sentence == nil + return nil + end + + # #Take in string and convert to array of words with spaces perseved + # sentence_array = my_sentence.split(/\s/) + + # #Iterate through array swapping each words + # i = 0 + # j = sentence_array.length - 1 + # while i < j + # temp_index = sentence_array[i] + # sentence_array[i] = sentence_array[j] + # sentence_array[j] = temp_index + + # i += 1 + # j -= 1 + # end + + # #Concatente elements of array into a new string output using (.join) + # new_sentence = sentence_array.join(' ') + + # #iterate through new string and replace each character into parameter, my_sentence + # new_sentence.length.times do |i| + # my_sentence[i] = new_sentence[i] + # end + + # return my_sentence + + ############ Second Try at Problem ############# + + # Step 1 - Reverse sentence in its entirity + # Create helper method to do this + def reverse(string) + start_point = 0 + end_point = string.length - 1 + while start_point < end_point + temp = string[start_point] + string[start_point] = string[end_point] + string[end_point] = temp + start_point += 1 + end_point -= 1 + end + return string + end + + # Pass in my_sentence into Step 1 helper method + reverse(my_sentence) + + # Step 2 Identify 'words' in sentence + # Identify start and end positions of each word + i = 0 + start_point = 0 + while i < my_sentence.length + if my_sentence[i] == " " + end_point = i - 1 + + while start_point < end_point + temp = my_sentence[start_point] + my_sentence[start_point] = my_sentence[end_point] + my_sentence[end_point] = temp + start_point += 1 + end_point -= 1 + end + start_point = i + 1 + + elsif i == my_sentence.length - 1 + end_point = i + while start_point < end_point + temp = my_sentence[start_point] + my_sentence[start_point] = my_sentence[end_point] + my_sentence[end_point] = temp + start_point += 1 + end_point -= 1 + end + + end + i += 1 + end + return my_sentence end diff --git a/lib/sort_by_length.rb b/lib/sort_by_length.rb index a5713ad..2b0b107 100644 --- a/lib/sort_by_length.rb +++ b/lib/sort_by_length.rb @@ -1,7 +1,35 @@ # 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) where n is the length of the array where the array consists of the words in the sentence as the elements of the array. +# Space complexity: O(n) because a new array is created from the words that make up the sentence string. def sort_by_length(my_sentence) - raise NotImplementedError, "Method not implemented" + if my_sentence.length == nil + return [] + end + + # split string sentence into array (took this from my Exercism assignment) + my_sentence = my_sentence.split(/[^\w']+/) + + # iterate through my_sentence array + # if word.length at index > word.length at index+1 + # swap words + # else keep words in place + # iterate to next word + # end + + i = 0 + while i < (my_sentence.length - 1) + j = 0 + while j < my_sentence.length - i - 1 + if (my_sentence[j].length) > (my_sentence[j+1]).length + temp = my_sentence[j] + my_sentence[j] = my_sentence[j+1] + my_sentence[j+1] = temp + end + j += 1 + end + i += 1 + end + + return my_sentence end diff --git a/test/reverse_sentence_test.rb b/test/reverse_sentence_test.rb index 346069b..d8e5767 100644 --- a/test/reverse_sentence_test.rb +++ b/test/reverse_sentence_test.rb @@ -6,8 +6,8 @@ test_string = "hello, world" reverse_sentence(test_string) - - test_string.must_equal "world hello," + + _(test_string).must_equal "world hello," end it "reverse a sentence with three words" do @@ -15,7 +15,7 @@ reverse_sentence(test_string) - test_string.must_equal "awesome! is Yoda" + _(test_string).must_equal "awesome! is Yoda" end end @@ -27,7 +27,7 @@ reverse_sentence(test_string) - test_string.must_be_empty + _(test_string).must_be_empty end # if the parameter is an object, check for nil @@ -36,7 +36,7 @@ reverse_sentence(test_string) - test_string.must_be_nil + _(test_string).must_be_nil end it "reverse a sentence with one word" do @@ -44,7 +44,7 @@ reverse_sentence(test_string) - test_string.must_equal "world" + _(test_string).must_equal "world" end it "reverse a sentence with multiple words" do @@ -52,7 +52,7 @@ 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" end it "reverse a sentence with multiple spaces between words" do @@ -60,15 +60,15 @@ reverse_sentence(test_string) - test_string.must_equal "apples? them like you do How" + _(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 " + _(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 diff --git a/test/test_helper.rb b/test/test_helper.rb index fdc67e2..bbb94b5 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,5 +1,6 @@ require 'minitest/autorun' require 'minitest/reporters' +require 'minitest/skip_dsl' require_relative '../lib/reverse_sentence' Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new