diff --git a/lib/reverse_sentence.rb b/lib/reverse_sentence.rb index 3fe7cac..bd403c3 100644 --- a/lib/reverse_sentence.rb +++ b/lib/reverse_sentence.rb @@ -1,6 +1,61 @@ # A method to reverse the words in a sentence, in place. -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n ^2) because lines 47-55 contain a nested loop. +# Space complexity: ? O(n) because of the empty_index_positions array from lines 33 - 38 + +def swap_chars(arr, index_one, index_two) + tmp = arr[index_two] + arr[index_two] = arr[index_one] + arr[index_one] = tmp +end + +def reverse_sub_str(array, start_position, end_position) + ((end_position - start_position + 1) / 2).times do + swap_chars(array, start_position, end_position) + start_position += 1 + end_position -= 1 + end + return array +end + def reverse_sentence(my_sentence) - raise NotImplementedError + + if my_sentence == "" + return "" + elsif my_sentence == nil + return nil + end + + + i = 0 + j = (my_sentence.length) -1 + reverse_sub_str(my_sentence, i, j) + + empty_index_positions = [] + my_sentence.length.times do |i| + if my_sentence[i] == " " + empty_index_positions << i + end + end + + if empty_index_positions.length == 0 + i = 0 + j = (my_sentence.length) -1 + reverse_sub_str(my_sentence, i, j) + return my_sentence + end + + empty_index_positions.length.times do |i| + if i == 0 + reverse_sub_str(my_sentence, i, empty_index_positions[i] - 1) + else + start_position = empty_index_positions[i - 1] + 1 + end_position = empty_index_positions[i] - 1 + reverse_sub_str(my_sentence, start_position, end_position) + end + end + + i = empty_index_positions[-1] + 1 + j = my_sentence.length - 1 + reverse_sub_str(my_sentence, i, j ) + return my_sentence end diff --git a/lib/sort_by_length.rb b/lib/sort_by_length.rb index a5713ad..862d1f9 100644 --- a/lib/sort_by_length.rb +++ b/lib/sort_by_length.rb @@ -1,7 +1,30 @@ # 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) because of splitting the string at the beginning of the method + +def swap(arr, index_one, index_two) + tmp = arr[index_two] + arr[index_two] = arr[index_one] + arr[index_one] = tmp +end + + def sort_by_length(my_sentence) - raise NotImplementedError, "Method not implemented" + my_sentence = my_sentence.split + + j = 0 # outer counter + my_sentence.length.times do + comparison_word = my_sentence[j] + for i in j...(my_sentence.length) + if my_sentence[i].length < comparison_word.length + puts my_sentence[i] + puts comparison_word + swap(my_sentence, i, j ) + comparison_word = my_sentence[j] + end + end + j += 1 + end + return my_sentence end diff --git a/test/reverse_sentence_test.rb b/test/reverse_sentence_test.rb index 346069b..02400d2 100644 --- a/test/reverse_sentence_test.rb +++ b/test/reverse_sentence_test.rb @@ -4,70 +4,70 @@ 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