From 4c273703db59d557b3eee45caa76ff4cbd806b1d Mon Sep 17 00:00:00 2001 From: geli-gel Date: Thu, 19 Sep 2019 12:05:43 -0700 Subject: [PATCH 1/5] renamed sort_by_length.rb to sort_by_length_test.rb --- test/{sort_by_length.rb => sort_by_length_test.rb} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename test/{sort_by_length.rb => sort_by_length_test.rb} (99%) 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..e5103c1 100644 --- a/test/sort_by_length.rb +++ b/test/sort_by_length_test.rb @@ -16,4 +16,4 @@ 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 -end \ No newline at end of file +end From a44a607c65e91147ffa061e5aa134f0d4d47f55f Mon Sep 17 00:00:00 2001 From: geli-gel Date: Mon, 23 Sep 2019 14:29:45 -0700 Subject: [PATCH 2/5] sort by length working, need to add complexity answers and remove excess comments --- lib/sort_by_length.rb | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/lib/sort_by_length.rb b/lib/sort_by_length.rb index a5713ad..036eafb 100644 --- a/lib/sort_by_length.rb +++ b/lib/sort_by_length.rb @@ -3,5 +3,36 @@ # Time complexity: ? # Space complexity: ? def sort_by_length(my_sentence) - raise NotImplementedError, "Method not implemented" + # split my_sentence by words assuming spaces separate them + # insertion_sort them in here, returning array of words + + words = my_sentence.split(" ") + puts words + # loop through from 2nd item to end, outer loop increasing by one, + i = 1 + while i < words.length + puts i + puts words.length + # set variable equal to "to_be_inserted" + to_be_inserted = words[i] + puts to_be_inserted + j = i + # from j to the beginning, and until j is less than value at [j-1] + while j > 0 && words[j-1].length > to_be_inserted.length + # check the value at [j-1], if value at [j-1] is greater than value at [j] then swap their values + if words[j-1].length > words[j].length + words[j], words[j-1] = words[j-1], words[j] + end + j -= 1 + puts words + puts j + end + words[j] = to_be_inserted + i += 1 + # reloop checking if now j is not greater than spot at left, then insert it there + # how to insert?? + # we say "words[j] = to_be_inserted" ... should work ?? + end + puts words + return words end From c7fa62a930223ab50c239d2276bb6aa8b3bbb5b0 Mon Sep 17 00:00:00 2001 From: geli-gel Date: Mon, 23 Sep 2019 18:19:18 -0700 Subject: [PATCH 3/5] tests passing --- lib/reverse_sentence.rb | 41 ++++++++++++++++++++++++++++++++++++++--- lib/sort_by_length.rb | 15 ++------------- 2 files changed, 40 insertions(+), 16 deletions(-) diff --git a/lib/reverse_sentence.rb b/lib/reverse_sentence.rb index 3fe7cac..8a2341b 100644 --- a/lib/reverse_sentence.rb +++ b/lib/reverse_sentence.rb @@ -1,6 +1,41 @@ # A method to reverse the words in a sentence, in place. -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n + n/2 + n + n/2) = O(n) + # it basically goes n times with n being how long the + # sentence is (in letters) +# Space complexity: O(left and right and regex and reversed words) = O(n) + # it will need to create a left and right and regex variable each time that + # won't change depending on input, but the list of words will increase by n + # if n increases (in number of words) def reverse_sentence(my_sentence) - raise NotImplementedError + return if my_sentence == nil || my_sentence.length <= 1 + # loop through my_sentence, reversing it from last to first + left = 0 + right = my_sentence.length - 1 + until left == right || right < left + my_sentence[left], my_sentence[right] = my_sentence[right], my_sentence[left] + left += 1 + right -= 1 + end + + # find all starts of reversed words and the words + regex = /([^ ]+)/ + # gets positions of starts of each reversed word + reversed_words = my_sentence.scan(regex).flatten + reversed_word_start_indices = my_sentence.enum_for(:scan, regex).map { Regexp.last_match.begin(0) } + if reversed_word_start_indices == nil + return my_sentence + end + + # for each start of word: + reversed_word_start_indices.each_with_index do |word_start_index, match_index| + # now that the sentence is reversed the words need to be reversed + # which will put them back in order. + left = word_start_index + right = left + reversed_words[match_index].length - 1 + until left == right || right < left + my_sentence[left], my_sentence[right] = my_sentence[right], my_sentence[left] + left += 1 + right -= 1 + end + end end diff --git a/lib/sort_by_length.rb b/lib/sort_by_length.rb index 036eafb..d9b0d6a 100644 --- a/lib/sort_by_length.rb +++ b/lib/sort_by_length.rb @@ -1,21 +1,16 @@ # 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) loop within loop, worst case same length +# Space complexity: O(1) - only creates i and j variables each time no matter input length def sort_by_length(my_sentence) # split my_sentence by words assuming spaces separate them # insertion_sort them in here, returning array of words - words = my_sentence.split(" ") - puts words # loop through from 2nd item to end, outer loop increasing by one, i = 1 while i < words.length - puts i - puts words.length # set variable equal to "to_be_inserted" to_be_inserted = words[i] - puts to_be_inserted j = i # from j to the beginning, and until j is less than value at [j-1] while j > 0 && words[j-1].length > to_be_inserted.length @@ -24,15 +19,9 @@ def sort_by_length(my_sentence) words[j], words[j-1] = words[j-1], words[j] end j -= 1 - puts words - puts j end words[j] = to_be_inserted i += 1 - # reloop checking if now j is not greater than spot at left, then insert it there - # how to insert?? - # we say "words[j] = to_be_inserted" ... should work ?? end - puts words return words end From bd3fce2b18fdc82171abde34674ffc40c994aa61 Mon Sep 17 00:00:00 2001 From: geli-gel Date: Mon, 23 Sep 2019 23:07:52 -0700 Subject: [PATCH 4/5] removed unnecessary if block --- lib/reverse_sentence.rb | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/reverse_sentence.rb b/lib/reverse_sentence.rb index 8a2341b..626d49e 100644 --- a/lib/reverse_sentence.rb +++ b/lib/reverse_sentence.rb @@ -22,9 +22,6 @@ def reverse_sentence(my_sentence) # gets positions of starts of each reversed word reversed_words = my_sentence.scan(regex).flatten reversed_word_start_indices = my_sentence.enum_for(:scan, regex).map { Regexp.last_match.begin(0) } - if reversed_word_start_indices == nil - return my_sentence - end # for each start of word: reversed_word_start_indices.each_with_index do |word_start_index, match_index| From b5e3d2fd446a1f9db865c2d622d39f2958e98860 Mon Sep 17 00:00:00 2001 From: geli-gel Date: Mon, 23 Sep 2019 23:54:33 -0700 Subject: [PATCH 5/5] added source for match indices code --- lib/reverse_sentence.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/reverse_sentence.rb b/lib/reverse_sentence.rb index 626d49e..ba7a426 100644 --- a/lib/reverse_sentence.rb +++ b/lib/reverse_sentence.rb @@ -19,8 +19,10 @@ def reverse_sentence(my_sentence) # find all starts of reversed words and the words regex = /([^ ]+)/ - # gets positions of starts of each reversed word reversed_words = my_sentence.scan(regex).flatten + # positions of regex matches with enum_for code originally from "Sean Hill"'s answer at + # https://stackoverflow.com/questions/5241653/ruby-regex-match-and-get-positions-of + # gets positions of starts of each matched reversed word reversed_word_start_indices = my_sentence.enum_for(:scan, regex).map { Regexp.last_match.begin(0) } # for each start of word: