From cd14919322fdcf459bd863a8d0260640e14d2050 Mon Sep 17 00:00:00 2001 From: Ga-Young Jin Date: Thu, 19 Sep 2019 11:53:27 -0700 Subject: [PATCH 1/4] completed sort by length method --- lib/sort_by_length.rb | 17 ++++++++++++++++- ...sort_by_length.rb => sort_by_length_test.rb} | 4 ++-- 2 files changed, 18 insertions(+), 3 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..b4dba87 100644 --- a/lib/sort_by_length.rb +++ b/lib/sort_by_length.rb @@ -3,5 +3,20 @@ # Time complexity: ? # Space complexity: ? def sort_by_length(my_sentence) - raise NotImplementedError, "Method not implemented" + # using bubble sort + sentence_in_array = my_sentence.split(" ") + i = 0 + while i < sentence_in_array.length-1 + j = 0 + while j < sentence_in_array.length-i-1 + if sentence_in_array[j].length > sentence_in_array[j+1].length + temp = sentence_in_array[j] + sentence_in_array[j] = sentence_in_array[j+1] + sentence_in_array[j+1] = temp + end + j += 1 + end + i += 1 + end + return sentence_in_array 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 1da3ce394e62b14ff621d5b49f60b62691547371 Mon Sep 17 00:00:00 2001 From: Ga-Young Jin Date: Thu, 19 Sep 2019 11:58:07 -0700 Subject: [PATCH 2/4] added time and space complexity for sort by length --- lib/sort_by_length.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/sort_by_length.rb b/lib/sort_by_length.rb index b4dba87..e307349 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) where n is the length of array +# Space complexity: O(n) where n is length of string/array def sort_by_length(my_sentence) # using bubble sort sentence_in_array = my_sentence.split(" ") From b389468f10fb6664db020edcb1bad842d233b4fb Mon Sep 17 00:00:00 2001 From: Ga-Young Jin Date: Mon, 30 Sep 2019 01:29:16 -0700 Subject: [PATCH 3/4] Completed reverse_sentence --- .DS_Store | Bin 0 -> 6148 bytes lib/reverse_sentence.rb | 29 ++++++++++++++++++--- test/reverse_sentence_test.rb | 46 +++++++++++++++++----------------- 3 files changed, 48 insertions(+), 27 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..10f4d43063cf2afdea345ed0e8725c067b6d3ef7 GIT binary patch literal 6148 zcmeH~O=`nH427SXECStlndNM9fZkvT$q90S5(*^{5-7CmIeMRdHg&o#raXc4Mj8v- z-@;=7u>I%T3orrL&|R_fFf(Jm!W9>szfK>w>;3l5idTWBh?%i6VYXk}5)lvq5fA|p z5P<~|$Wt7f=LJ2J9z_I1U>OAb`_SmFy>z6;r-LCz0P33MFs@^kpf)d1d+A7Jg=RH9 zShZS=AzqJmYOCvd=}66XSPdUmcQ&75XqN4;#)M`)L_q{ZU`Ak-`Q+#Sk^bBKKWkAc z0wVCw2-x~?I_&vUb+$gdp4VTi>gz$L#^nq@egc^IQM{#xaliS3+Dk_&D>VHG1O^2W H_)`MkvH%f) literal 0 HcmV?d00001 diff --git a/lib/reverse_sentence.rb b/lib/reverse_sentence.rb index 3fe7cac..7eb0008 100644 --- a/lib/reverse_sentence.rb +++ b/lib/reverse_sentence.rb @@ -1,6 +1,27 @@ # A method to reverse the words in a sentence, in place. -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) where n is the length of string +# Space complexity: O(n) where n is the length of string + def reverse_sentence(my_sentence) - raise NotImplementedError -end + if my_sentence == nil + return nil + end + + char_count = 0 + position_count = my_sentence.length + 1 + + while char_count < my_sentence.length + if my_sentence[0] != " " + my_sentence.insert(position_count - 1, my_sentence[0]) + my_sentence[0] = "" + char_count += 1 + else + position_count = my_sentence.length-char_count + my_sentence.insert(position_count, " ") + my_sentence[0] = "" + char_count += 1 + end + end + + return my_sentence +end \ No newline at end of file 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 cc7db93b545426181dce3c31a6a0256b7412a252 Mon Sep 17 00:00:00 2001 From: Ga-Young Jin Date: Mon, 30 Sep 2019 01:38:24 -0700 Subject: [PATCH 4/4] changed time complexity for reverse sentence --- lib/reverse_sentence.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/reverse_sentence.rb b/lib/reverse_sentence.rb index 7eb0008..6a6a324 100644 --- a/lib/reverse_sentence.rb +++ b/lib/reverse_sentence.rb @@ -1,5 +1,5 @@ # A method to reverse the words in a sentence, in place. -# Time complexity: O(n) where n is the length of string +# Time complexity: O(n^2) where n is the length of string # Space complexity: O(n) where n is the length of string def reverse_sentence(my_sentence)