diff --git a/lib/reverse_sentence.rb b/lib/reverse_sentence.rb index 3fe7cac..ba7a426 100644 --- a/lib/reverse_sentence.rb +++ b/lib/reverse_sentence.rb @@ -1,6 +1,40 @@ # 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 = /([^ ]+)/ + 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: + 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 a5713ad..d9b0d6a 100644 --- a/lib/sort_by_length.rb +++ b/lib/sort_by_length.rb @@ -1,7 +1,27 @@ # 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) - 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(" ") + # loop through from 2nd item to end, outer loop increasing by one, + i = 1 + while i < words.length + # set variable equal to "to_be_inserted" + to_be_inserted = words[i] + 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 + end + words[j] = to_be_inserted + i += 1 + end + return words end 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