From dba81cc6060bee43d554036ff3ed0862fb52d4fb Mon Sep 17 00:00:00 2001 From: Mariya Burrows Date: Mon, 23 Sep 2019 20:23:43 -0700 Subject: [PATCH 1/3] completed sort_by_length method --- lib/sort_by_length.rb | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/sort_by_length.rb b/lib/sort_by_length.rb index a5713ad..24cf17c 100644 --- a/lib/sort_by_length.rb +++ b/lib/sort_by_length.rb @@ -2,6 +2,29 @@ # sorted by the length of the word. # Time complexity: ? # Space complexity: ? + +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 From a00d11cff10ddc4231817f56d3f9a2d6d172943b Mon Sep 17 00:00:00 2001 From: Mariya Burrows Date: Wed, 25 Sep 2019 09:11:56 -0700 Subject: [PATCH 2/3] completed wave 2 --- lib/reverse_sentence.rb | 68 ++++++++++++++++++++++++++++++++++- lib/sort_by_length.rb | 4 +-- test/reverse_sentence_test.rb | 46 ++++++++++++------------ 3 files changed, 92 insertions(+), 26 deletions(-) diff --git a/lib/reverse_sentence.rb b/lib/reverse_sentence.rb index 3fe7cac..b9c311c 100644 --- a/lib/reverse_sentence.rb +++ b/lib/reverse_sentence.rb @@ -1,6 +1,72 @@ # A method to reverse the words in a sentence, in place. # Time complexity: ? # Space complexity: ? + +def swap_chars(arr, index_one, index_two) + tmp = arr[index_two] + arr[index_two] = arr[index_one] + arr[index_one] = tmp +end + +# def find_empty_space(array) +# i = 0 +# until array[i] == " " +# i += 1 +# end +# return i +# 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) + # p my_sentence + + + empty_index_positions = [] + my_sentence.length.times do |i| + if my_sentence[i] == " " + empty_index_positions << i + end + end + # p empty_index_positions + + 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 24cf17c..862d1f9 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(n) because of splitting the string at the beginning of the method def swap(arr, index_one, index_two) tmp = arr[index_two] 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 From 41c4de55aabf116b04127cd2b8f63cd4c5ce9783 Mon Sep 17 00:00:00 2001 From: Mariya Burrows Date: Wed, 25 Sep 2019 09:35:05 -0700 Subject: [PATCH 3/3] added time & space complexity --- lib/reverse_sentence.rb | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/lib/reverse_sentence.rb b/lib/reverse_sentence.rb index b9c311c..bd403c3 100644 --- a/lib/reverse_sentence.rb +++ b/lib/reverse_sentence.rb @@ -1,6 +1,6 @@ # 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] @@ -8,14 +8,6 @@ def swap_chars(arr, index_one, index_two) arr[index_one] = tmp end -# def find_empty_space(array) -# i = 0 -# until array[i] == " " -# i += 1 -# end -# return i -# 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) @@ -37,8 +29,6 @@ def reverse_sentence(my_sentence) i = 0 j = (my_sentence.length) -1 reverse_sub_str(my_sentence, i, j) - # p my_sentence - empty_index_positions = [] my_sentence.length.times do |i| @@ -46,7 +36,6 @@ def reverse_sentence(my_sentence) empty_index_positions << i end end - # p empty_index_positions if empty_index_positions.length == 0 i = 0