From bd833f3610592180f2ac195dd52f120524e4b37c Mon Sep 17 00:00:00 2001 From: KKennedyCodes Date: Fri, 27 Sep 2019 08:39:15 -0700 Subject: [PATCH 1/5] Passing First Set of Tests --- lib/sort_by_length.rb | 24 ++++- test/reverse_sentence_test.rb | 98 +++++++++---------- ...rt_by_length.rb => sort_by_length_test.rb} | 4 +- 3 files changed, 74 insertions(+), 52 deletions(-) rename test/{sort_by_length.rb => sort_by_length_test.rb} (99%) diff --git a/lib/sort_by_length.rb b/lib/sort_by_length.rb index a5713ad..8e89041 100644 --- a/lib/sort_by_length.rb +++ b/lib/sort_by_length.rb @@ -2,6 +2,28 @@ # sorted by the length of the word. # Time complexity: ? # Space complexity: ? + +require 'pry' def sort_by_length(my_sentence) - raise NotImplementedError, "Method not implemented" + sorted_sentence = my_sentence.split(" ") + bubble_sort(sorted_sentence, sorted_sentence.length) + return sorted_sentence end + +def bubble_sort(array, length) + i = 0 + while i < length-1 # outer loop + j = 0 + 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 + end + j += 1 + end + i += 1 + end +end + +p sort_by_length("Basketball is life!") \ No newline at end of file diff --git a/test/reverse_sentence_test.rb b/test/reverse_sentence_test.rb index 346069b..461fd9e 100644 --- a/test/reverse_sentence_test.rb +++ b/test/reverse_sentence_test.rb @@ -1,74 +1,74 @@ require_relative "test_helper" -describe "reverse sentence" do - describe "basic tests" do - it "reverse a sentence with two words" do - test_string = "hello, world" +# describe "reverse sentence" do +# describe "basic tests" do +# it "reverse a sentence with two words" do +# test_string = "hello, world" - reverse_sentence(test_string) +# reverse_sentence(test_string) - test_string.must_equal "world hello," - end +# test_string.must_equal "world hello," +# end - it "reverse a sentence with three words" do - test_string = "Yoda is awesome!" +# it "reverse a sentence with three words" do +# test_string = "Yoda is awesome!" - reverse_sentence(test_string) +# reverse_sentence(test_string) - test_string.must_equal "awesome! is Yoda" - end - end +# 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 = "" +# # 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) +# reverse_sentence(test_string) - test_string.must_be_empty - end +# 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 +# # if the parameter is an object, check for nil +# it "nil object passed to sentence reverse" do +# test_string = nil - reverse_sentence(test_string) +# reverse_sentence(test_string) - test_string.must_be_nil - end +# test_string.must_be_nil +# end - it "reverse a sentence with one word" do - test_string = "world" +# it "reverse a sentence with one word" do +# test_string = "world" - reverse_sentence(test_string) +# reverse_sentence(test_string) - test_string.must_equal "world" - end +# 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." +# it "reverse a sentence with multiple words" do +# test_string = "I'm a better engineer today than I was yesterday." - reverse_sentence(test_string) +# reverse_sentence(test_string) - test_string.must_equal "yesterday. was I than today engineer better a I'm" - end +# 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?" +# it "reverse a sentence with multiple spaces between words" do +# test_string = "How do you like them apples?" - reverse_sentence(test_string) +# reverse_sentence(test_string) - test_string.must_equal "apples? them like you do How" - end +# 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! " +# it "reverse a sentence with preceeding and trailing white spaces" do +# test_string = " I can do this! " - reverse_sentence(test_string) +# reverse_sentence(test_string) - test_string.must_equal " this! do can I " - end - end -end +# test_string.must_equal " this! do can I " +# end +# end +# 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..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 From e3fe1a4f28dee0ef08b419508d380f62db4314c6 Mon Sep 17 00:00:00 2001 From: KKennedyCodes Date: Fri, 27 Sep 2019 09:26:25 -0700 Subject: [PATCH 2/5] Doing what I want in IRB, but not passing the tests. --- lib/reverse_sentence.rb | 29 ++++++- test/reverse_sentence_test.rb | 144 +++++++++++++++++----------------- test/sort_by_length_test.rb | 34 ++++---- 3 files changed, 117 insertions(+), 90 deletions(-) diff --git a/lib/reverse_sentence.rb b/lib/reverse_sentence.rb index 3fe7cac..6097da3 100644 --- a/lib/reverse_sentence.rb +++ b/lib/reverse_sentence.rb @@ -1,6 +1,33 @@ # A method to reverse the words in a sentence, in place. # Time complexity: ? # Space complexity: ? + + def reverse_sentence(my_sentence) - raise NotImplementedError + reverse_sentence = my_sentence.split(" ") + bubble_sort(reverse_sentence, reverse_sentence.length) + reversed = reverse_sentence.join(" ") + return reversed end + +def bubble_sort(array, length) + i = 0 + while i < length-1 # outer loop + j = 0 + while j < length-i-1 # inner loop + p array.index(array[j]) + p array.index(array[j+1]) + if array.index(array[j]) < array.index(array[j+1]) # swap + temp = array[j] + array[j] = array[j+1] + array[j+1] = temp + end + j += 1 + end + i += 1 + end +end + +test_string = "hello, world" + +p reverse_sentence(test_string) \ No newline at end of file diff --git a/test/reverse_sentence_test.rb b/test/reverse_sentence_test.rb index 461fd9e..22028c4 100644 --- a/test/reverse_sentence_test.rb +++ b/test/reverse_sentence_test.rb @@ -1,74 +1,74 @@ require_relative "test_helper" -# describe "reverse sentence" do -# 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 +describe "reverse sentence" do + 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 diff --git a/test/sort_by_length_test.rb b/test/sort_by_length_test.rb index 1eb292f..11a2941 100644 --- a/test/sort_by_length_test.rb +++ b/test/sort_by_length_test.rb @@ -1,19 +1,19 @@ require_relative "test_helper" -describe "sort_by_length" do - it "will return an empty array for an empty string" do - expect(sort_by_length("")).must_equal [] - end - - 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 -end \ No newline at end of file +# describe "sort_by_length" do +# it "will return an empty array for an empty string" do +# expect(sort_by_length("")).must_equal [] +# end + +# 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 +# end \ No newline at end of file From 1b9ef193c6597c04455c5addcd3fff6af6a5c65c Mon Sep 17 00:00:00 2001 From: KKennedyCodes Date: Sun, 3 Nov 2019 12:38:22 -0800 Subject: [PATCH 3/5] Passing Tests on First Exercises --- .DS_Store | Bin 0 -> 6148 bytes lib/reverse_sentence.rb | 56 ++++++------- lib/sort_by_length.rb | 7 +- test/reverse_sentence_test.rb | 148 +++++++++++++++++----------------- test/sort_by_length_test.rb | 34 ++++---- 5 files changed, 121 insertions(+), 124 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..e7f3a151198e9e964bedc6ede0248701d24d73d0 GIT binary patch literal 6148 zcmeHKO^ee&7=CA4ZL$T4Ah?2u0S|g8QfYM$USe$(K~Xk~d+?(sCToK+S(0>nD1{(+ z^#|zH5BwYIkMMtpXMN^F*?#Cz#GM!3dEU%C?;GZ6XC^~LtTzrX5IIC-!3EaW(X22r zE_}(>bWa1wWQ~{-if$y?;~-gLn+~IZQQ%)wK&;&cU8jhi(JmcWzgd70u`OJNpZhKi z5m60mJf=Xc@?WeEt69OM*b5TL`WEaZaZ$G0U)Wl$e&lGwve%n-^H|e<;El40SNi3+ z==u53Iqk(^6!`g09G0W)C>Ad^@5E`CO?ufl4GYQF#btcQFGr|jopF7R2@u(qJ#;pv(@y7Ni+8%+S&L6MH`^`V z?OdA8Y|A=z_S)9N@$;9jUcY(!?)}G4GK(0tKWWD`{)Ep6v|#&Qo+erP1U-Un(H8mi z6vD5Rot*^dpzJpI1G++ex&=IspDUdI3D}E;j5?L5UQoHf`?|oxqp`!Nfd123&BHi( zGiRfKQQ(gXi2Z@X1-cq5g>vgaBd-9!I=ZDH79R~Fhih~-Rthl!V=5J>QklMDFqMve zu6eG;N})<8rY|2%znSS93X^X~eJ<(5T!p4J3K#|E6{x94N1XqI-{1e|lgyJ*z$oxv zDZuJ|uiwLz^x3*JIdRr<_#?P5!K)O?6g2ucmVr2mH{jAR#zg>hHC77I12Z22QU=o) J1^%i6KLEaPo2~!= literal 0 HcmV?d00001 diff --git a/lib/reverse_sentence.rb b/lib/reverse_sentence.rb index 6097da3..323ad7a 100644 --- a/lib/reverse_sentence.rb +++ b/lib/reverse_sentence.rb @@ -1,33 +1,33 @@ -# A method to reverse the words in a sentence, in place. -# Time complexity: ? -# Space complexity: ? +# # A method to reverse the words in a sentence, in place. +# # Time complexity: ? +# # Space complexity: ? -def reverse_sentence(my_sentence) - reverse_sentence = my_sentence.split(" ") - bubble_sort(reverse_sentence, reverse_sentence.length) - reversed = reverse_sentence.join(" ") - return reversed -end +# def reverse_sentence(my_sentence) +# reverse_sentence = my_sentence.split(" ") +# bubble_sort(reverse_sentence, reverse_sentence.length) +# reversed = reverse_sentence.join(" ") +# return reversed +# end -def bubble_sort(array, length) - i = 0 - while i < length-1 # outer loop - j = 0 - while j < length-i-1 # inner loop - p array.index(array[j]) - p array.index(array[j+1]) - if array.index(array[j]) < array.index(array[j+1]) # swap - temp = array[j] - array[j] = array[j+1] - array[j+1] = temp - end - j += 1 - end - i += 1 - end -end +# def bubble_sort(array, length) +# i = 0 +# while i < length-1 # outer loop +# j = 0 +# while j < length-i-1 # inner loop +# p array.index(array[j]) +# p array.index(array[j+1]) +# if array.index(array[j]) < array.index(array[j+1]) # swap +# temp = array[j] +# array[j] = array[j+1] +# array[j+1] = temp +# end +# j += 1 +# end +# i += 1 +# end +# end -test_string = "hello, world" +# test_string = "hello, world" -p reverse_sentence(test_string) \ No newline at end of file +# p reverse_sentence(test_string) \ No newline at end of file diff --git a/lib/sort_by_length.rb b/lib/sort_by_length.rb index 8e89041..e0fc8e2 100644 --- a/lib/sort_by_length.rb +++ b/lib/sort_by_length.rb @@ -1,9 +1,8 @@ # 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: On -require 'pry' def sort_by_length(my_sentence) sorted_sentence = my_sentence.split(" ") bubble_sort(sorted_sentence, sorted_sentence.length) @@ -25,5 +24,3 @@ def bubble_sort(array, length) i += 1 end end - -p sort_by_length("Basketball is life!") \ No newline at end of file diff --git a/test/reverse_sentence_test.rb b/test/reverse_sentence_test.rb index 22028c4..c5b0af6 100644 --- a/test/reverse_sentence_test.rb +++ b/test/reverse_sentence_test.rb @@ -1,74 +1,74 @@ -require_relative "test_helper" - -describe "reverse sentence" do - 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 +# require_relative "test_helper" + +# describe "reverse sentence" do +# 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 diff --git a/test/sort_by_length_test.rb b/test/sort_by_length_test.rb index 11a2941..1eb292f 100644 --- a/test/sort_by_length_test.rb +++ b/test/sort_by_length_test.rb @@ -1,19 +1,19 @@ require_relative "test_helper" -# describe "sort_by_length" do -# it "will return an empty array for an empty string" do -# expect(sort_by_length("")).must_equal [] -# end - -# 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 -# end \ No newline at end of file +describe "sort_by_length" do + it "will return an empty array for an empty string" do + expect(sort_by_length("")).must_equal [] + end + + 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 +end \ No newline at end of file From 2418f3209ecbb649f0b3792dd9a4f45862775499 Mon Sep 17 00:00:00 2001 From: KKennedyCodes Date: Sun, 3 Nov 2019 13:22:56 -0800 Subject: [PATCH 4/5] Reverse sentence completed with tests passing --- lib/reverse_sentence.rb | 78 +++++++++++------- test/reverse_sentence_test.rb | 148 +++++++++++++++++----------------- 2 files changed, 121 insertions(+), 105 deletions(-) diff --git a/lib/reverse_sentence.rb b/lib/reverse_sentence.rb index 323ad7a..ce0452c 100644 --- a/lib/reverse_sentence.rb +++ b/lib/reverse_sentence.rb @@ -1,33 +1,49 @@ -# # A method to reverse the words in a sentence, in place. -# # Time complexity: ? -# # Space complexity: ? +def reverse_sentence(my_sentence) + if my_sentence == nil + return nil + end + my_sentence = string_reverse(my_sentence) + my_sentence = word_reverse(my_sentence) + return my_sentence +end +def string_reverse(string) + i = 0 + j = string.length - 1 + temp_i = "" + temp_j = "" + while i < j do + temp_i = string[i] + temp_j = string[j] + string[i] = temp_j + string[j] = temp_i + i+=1 + j-=1 + end + return string +end -# def reverse_sentence(my_sentence) -# reverse_sentence = my_sentence.split(" ") -# bubble_sort(reverse_sentence, reverse_sentence.length) -# reversed = reverse_sentence.join(" ") -# return reversed -# end - -# def bubble_sort(array, length) -# i = 0 -# while i < length-1 # outer loop -# j = 0 -# while j < length-i-1 # inner loop -# p array.index(array[j]) -# p array.index(array[j+1]) -# if array.index(array[j]) < array.index(array[j+1]) # swap -# temp = array[j] -# array[j] = array[j+1] -# array[j+1] = temp -# end -# j += 1 -# end -# i += 1 -# end -# end - -# test_string = "hello, world" - -# p reverse_sentence(test_string) \ No newline at end of file +def word_reverse(string) + i = 0 + j = 0 + length = string.length + while i < length + while string[j] != " " && j < length do + j += 1 + end + j -= 1 + word_start = i + word_end = j + while word_start < word_end do + temp_i = string[word_start] + temp_j = string[word_end] + string[word_start] = temp_j + string[word_end] = temp_i + word_start+=1 + word_end-=1 + end + i = j+2 + j = i + end + return string +end diff --git a/test/reverse_sentence_test.rb b/test/reverse_sentence_test.rb index c5b0af6..02400d2 100644 --- a/test/reverse_sentence_test.rb +++ b/test/reverse_sentence_test.rb @@ -1,74 +1,74 @@ -# require_relative "test_helper" - -# describe "reverse sentence" do -# 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 +require_relative "test_helper" + +describe "reverse sentence" do + 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 From 3cb0536d9f06bc5bfb862aea738876ff3d30ce98 Mon Sep 17 00:00:00 2001 From: KKennedyCodes Date: Sun, 3 Nov 2019 13:24:47 -0800 Subject: [PATCH 5/5] forgot space and time --- lib/reverse_sentence.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/reverse_sentence.rb b/lib/reverse_sentence.rb index ce0452c..13a9761 100644 --- a/lib/reverse_sentence.rb +++ b/lib/reverse_sentence.rb @@ -1,3 +1,6 @@ +# A method to reverse the words in a sentence, in place. +# Time complexity: O(n^3) +# Space complexity: O(1) def reverse_sentence(my_sentence) if my_sentence == nil return nil