diff --git a/lib/reverse_sentence.rb b/lib/reverse_sentence.rb index 3fe7cac..f1b5e0f 100644 --- a/lib/reverse_sentence.rb +++ b/lib/reverse_sentence.rb @@ -1,6 +1,54 @@ # A method to reverse the words in a sentence, in place. -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n^3) +# Space complexity: O(n) I believe since there is a second array created in memory to store/reverse each word +require 'pry' + def reverse_sentence(my_sentence) + if my_sentence == "" + return "" + elsif my_sentence == nil + return nil + end + + length = my_sentence.length + reversaroo(my_sentence, length) + # first fully reverse the strings + + # take each 'word' and split into an array. then reversaroo the each word and store in an array called word_in_order_array + split_sentence = my_sentence.split(' ') + word_in_order_array = [] + split_sentence.each_with_index do |word, i| + length = word.length + temp = reversaroo(word, length) + word_in_order_array.push(temp) + end + + x = 0 + word_in_order_array.each do |word| + word.each_char do |char| + until my_sentence[x] != " " + x += 1 + end + my_sentence[x] = char + x += 1 + end + end + # search for any character or whitespace followed immediately by a character + return my_sentence raise NotImplementedError end + + +def reversaroo(string, length) + j = (length - 1) + i = 0 + while i < j + temp = string[i] + string[i] = string[j] + string[j] = temp + j -= 1 + i += 1 + end + return string +end + diff --git a/lib/sort_by_length.rb b/lib/sort_by_length.rb index a5713ad..bf7e2ef 100644 --- a/lib/sort_by_length.rb +++ b/lib/sort_by_length.rb @@ -1,7 +1,36 @@ # 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(1) constat I believe def sort_by_length(my_sentence) + if my_sentence == "" + return [] + end + + array = my_sentence.split(' ') + # longest_string = array.first + length = array.length + + # If the inner loop runs with no swaps, exit + swapped = true + i = 0 + while i < length-1 && swapped # outer loop + j = 0 + # Assume you won't have to make a swap + swapped = false + while j < length-i-1 # inner loop + if array[j].length > array[j+1].length # swap + temp = array[j] + array[j] = array[j+1] + array[j+1] = temp + # Since we just made a swap, set swapped to true + swapped = true + end + j += 1 + end + i += 1 + end + + return array raise NotImplementedError, "Method not implemented" end diff --git a/test/reverse_sentence_test.rb b/test/reverse_sentence_test.rb index 346069b..5ed14ef 100644 --- a/test/reverse_sentence_test.rb +++ b/test/reverse_sentence_test.rb @@ -4,71 +4,58 @@ 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 -end + 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 + end + \ No newline at end of file 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..1eb292f 100644 --- a/test/sort_by_length.rb +++ b/test/sort_by_length_test.rb @@ -8,11 +8,11 @@ it "will return an array of words, by length" do expect(sort_by_length("I love Ada")).must_equal ["I", "Ada", "love"] end - + 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("words of equal length")).must_equal ["of", "words", "equal", "length"] end - + 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