From fe6319235ae1480adaa14076dc8bef7455718042 Mon Sep 17 00:00:00 2001 From: Farah Davoodi Date: Sun, 22 Sep 2019 22:37:07 -0700 Subject: [PATCH 1/6] Accidently cloned C12 file...adding sort-by-length method to my own repo --- lib/sort_by_length.rb | 34 +++++++++++++++++-- test/reverse_sentence_test.rb | 16 ++++----- ...rt_by_length.rb => sort_by_length_test.rb} | 0 test/test_helper.rb | 1 + 4 files changed, 40 insertions(+), 11 deletions(-) rename test/{sort_by_length.rb => sort_by_length_test.rb} (100%) diff --git a/lib/sort_by_length.rb b/lib/sort_by_length.rb index a5713ad..2b0b107 100644 --- a/lib/sort_by_length.rb +++ b/lib/sort_by_length.rb @@ -1,7 +1,35 @@ # 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 the array where the array consists of the words in the sentence as the elements of the array. +# Space complexity: O(n) because a new array is created from the words that make up the sentence string. def sort_by_length(my_sentence) - raise NotImplementedError, "Method not implemented" + if my_sentence.length == nil + return [] + end + + # split string sentence into array (took this from my Exercism assignment) + my_sentence = my_sentence.split(/[^\w']+/) + + # iterate through my_sentence array + # if word.length at index > word.length at index+1 + # swap words + # else keep words in place + # iterate to next word + # end + + i = 0 + while i < (my_sentence.length - 1) + j = 0 + while j < my_sentence.length - i - 1 + if (my_sentence[j].length) > (my_sentence[j+1]).length + temp = my_sentence[j] + my_sentence[j] = my_sentence[j+1] + my_sentence[j+1] = temp + end + j += 1 + end + i += 1 + end + + return my_sentence end diff --git a/test/reverse_sentence_test.rb b/test/reverse_sentence_test.rb index 346069b..8aea431 100644 --- a/test/reverse_sentence_test.rb +++ b/test/reverse_sentence_test.rb @@ -2,7 +2,7 @@ describe "reverse sentence" do describe "basic tests" do - it "reverse a sentence with two words" do + xit "reverse a sentence with two words" do test_string = "hello, world" reverse_sentence(test_string) @@ -10,7 +10,7 @@ test_string.must_equal "world hello," end - it "reverse a sentence with three words" do + xit "reverse a sentence with three words" do test_string = "Yoda is awesome!" reverse_sentence(test_string) @@ -22,7 +22,7 @@ # check for edge cases describe "edge cases" do # if it's a string parameter, check for empty - it "reverse an empty sentence" do + xit "reverse an empty sentence" do test_string = "" reverse_sentence(test_string) @@ -31,7 +31,7 @@ end # if the parameter is an object, check for nil - it "nil object passed to sentence reverse" do + xit "nil object passed to sentence reverse" do test_string = nil reverse_sentence(test_string) @@ -39,7 +39,7 @@ test_string.must_be_nil end - it "reverse a sentence with one word" do + xit "reverse a sentence with one word" do test_string = "world" reverse_sentence(test_string) @@ -47,7 +47,7 @@ test_string.must_equal "world" end - it "reverse a sentence with multiple words" do + xit "reverse a sentence with multiple words" do test_string = "I'm a better engineer today than I was yesterday." reverse_sentence(test_string) @@ -55,7 +55,7 @@ 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 + xit "reverse a sentence with multiple spaces between words" do test_string = "How do you like them apples?" reverse_sentence(test_string) @@ -63,7 +63,7 @@ test_string.must_equal "apples? them like you do How" end - it "reverse a sentence with preceeding and trailing white spaces" do + xit "reverse a sentence with preceeding and trailing white spaces" do test_string = " I can do this! " reverse_sentence(test_string) diff --git a/test/sort_by_length.rb b/test/sort_by_length_test.rb similarity index 100% rename from test/sort_by_length.rb rename to test/sort_by_length_test.rb diff --git a/test/test_helper.rb b/test/test_helper.rb index fdc67e2..bbb94b5 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,5 +1,6 @@ require 'minitest/autorun' require 'minitest/reporters' +require 'minitest/skip_dsl' require_relative '../lib/reverse_sentence' Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new From afa782de39ccd69ad28ce455c5e93d5b09475705 Mon Sep 17 00:00:00 2001 From: Farah Davoodi Date: Mon, 23 Sep 2019 00:09:48 -0700 Subject: [PATCH 2/6] Completed reverse method but cant pass tests --- lib/reverse_sentence.rb | 21 ++++++++++++++++++++- test/reverse_sentence_test.rb | 4 ++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/lib/reverse_sentence.rb b/lib/reverse_sentence.rb index 3fe7cac..ce140b8 100644 --- a/lib/reverse_sentence.rb +++ b/lib/reverse_sentence.rb @@ -1,6 +1,25 @@ +require 'pry' # A method to reverse the words in a sentence, in place. # Time complexity: ? # Space complexity: ? def reverse_sentence(my_sentence) - raise NotImplementedError + #Take in string and convert to array of words with spaces perseved + sentence_array = my_sentence.split(/[\s$]{1}/) + # binding.pry + #Iterate through array swapping words + i = 0 + j = sentence_array.length - 1 + while i < j + temp_index = sentence_array[i] + sentence_array[i] = sentence_array[j] + sentence_array[j] = temp_index + + i += 1 + j -= 1 + end + + my_sentence = sentence_array.join(' ') + binding.pry + return my_sentence + #Concatente elements of array into a string output that will be returned (.join) end diff --git a/test/reverse_sentence_test.rb b/test/reverse_sentence_test.rb index 8aea431..618d668 100644 --- a/test/reverse_sentence_test.rb +++ b/test/reverse_sentence_test.rb @@ -2,7 +2,7 @@ describe "reverse sentence" do describe "basic tests" do - xit "reverse a sentence with two words" do + it "reverse a sentence with two words" do test_string = "hello, world" reverse_sentence(test_string) @@ -10,7 +10,7 @@ test_string.must_equal "world hello," end - xit "reverse a sentence with three words" do + it "reverse a sentence with three words" do test_string = "Yoda is awesome!" reverse_sentence(test_string) From 28802a07b09a115bcab0e8bb9c962a225d0c87e2 Mon Sep 17 00:00:00 2001 From: Farah Davoodi Date: Mon, 23 Sep 2019 00:31:00 -0700 Subject: [PATCH 3/6] Passed test that check if nil or empty --- lib/reverse_sentence.rb | 10 ++++++---- test/reverse_sentence_test.rb | 12 ++++++------ 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/reverse_sentence.rb b/lib/reverse_sentence.rb index ce140b8..2669b73 100644 --- a/lib/reverse_sentence.rb +++ b/lib/reverse_sentence.rb @@ -3,9 +3,13 @@ # Time complexity: ? # Space complexity: ? def reverse_sentence(my_sentence) + if my_sentence == nil + return nil + end + #Take in string and convert to array of words with spaces perseved sentence_array = my_sentence.split(/[\s$]{1}/) - # binding.pry + #Iterate through array swapping words i = 0 j = sentence_array.length - 1 @@ -18,8 +22,6 @@ def reverse_sentence(my_sentence) j -= 1 end - my_sentence = sentence_array.join(' ') - binding.pry - return my_sentence + return my_sentence = sentence_array.join(' ') #Concatente elements of array into a string output that will be returned (.join) end diff --git a/test/reverse_sentence_test.rb b/test/reverse_sentence_test.rb index 618d668..346069b 100644 --- a/test/reverse_sentence_test.rb +++ b/test/reverse_sentence_test.rb @@ -22,7 +22,7 @@ # check for edge cases describe "edge cases" do # if it's a string parameter, check for empty - xit "reverse an empty sentence" do + it "reverse an empty sentence" do test_string = "" reverse_sentence(test_string) @@ -31,7 +31,7 @@ end # if the parameter is an object, check for nil - xit "nil object passed to sentence reverse" do + it "nil object passed to sentence reverse" do test_string = nil reverse_sentence(test_string) @@ -39,7 +39,7 @@ test_string.must_be_nil end - xit "reverse a sentence with one word" do + it "reverse a sentence with one word" do test_string = "world" reverse_sentence(test_string) @@ -47,7 +47,7 @@ test_string.must_equal "world" end - xit "reverse a sentence with multiple words" do + it "reverse a sentence with multiple words" do test_string = "I'm a better engineer today than I was yesterday." reverse_sentence(test_string) @@ -55,7 +55,7 @@ test_string.must_equal "yesterday. was I than today engineer better a I'm" end - xit "reverse a sentence with multiple spaces between words" do + it "reverse a sentence with multiple spaces between words" do test_string = "How do you like them apples?" reverse_sentence(test_string) @@ -63,7 +63,7 @@ test_string.must_equal "apples? them like you do How" end - xit "reverse a sentence with preceeding and trailing white spaces" do + it "reverse a sentence with preceeding and trailing white spaces" do test_string = " I can do this! " reverse_sentence(test_string) From 82dd0b2ad3305eaeb829f8243668b8099b800db9 Mon Sep 17 00:00:00 2001 From: Farah Davoodi Date: Sun, 29 Sep 2019 18:02:09 -0700 Subject: [PATCH 4/6] passed more tests and updated test file because of error minitest was says about deprecated...using _(obj).must_be_#### as opposed to just obj.must_be_#### --- lib/reverse_sentence.rb | 19 +++++++++++++------ test/reverse_sentence_test.rb | 20 ++++++++++---------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/lib/reverse_sentence.rb b/lib/reverse_sentence.rb index 2669b73..1180349 100644 --- a/lib/reverse_sentence.rb +++ b/lib/reverse_sentence.rb @@ -6,11 +6,11 @@ def reverse_sentence(my_sentence) if my_sentence == nil return nil end - + #Take in string and convert to array of words with spaces perseved - sentence_array = my_sentence.split(/[\s$]{1}/) - - #Iterate through array swapping words + sentence_array = my_sentence.split(/[\s]/) + + #Iterate through array swapping each words i = 0 j = sentence_array.length - 1 while i < j @@ -22,6 +22,13 @@ def reverse_sentence(my_sentence) j -= 1 end - return my_sentence = sentence_array.join(' ') - #Concatente elements of array into a string output that will be returned (.join) + #Concatente elements of array into a new string output using (.join) + new_sentence = sentence_array.join(' ') + + #iterate through new string and replace each character into parameter, my_sentence + new_sentence.length.times do |i| + my_sentence[i] = new_sentence[i] + end + + return my_sentence end diff --git a/test/reverse_sentence_test.rb b/test/reverse_sentence_test.rb index 346069b..d8e5767 100644 --- a/test/reverse_sentence_test.rb +++ b/test/reverse_sentence_test.rb @@ -6,8 +6,8 @@ test_string = "hello, world" reverse_sentence(test_string) - - test_string.must_equal "world hello," + + _(test_string).must_equal "world hello," end it "reverse a sentence with three words" do @@ -15,7 +15,7 @@ reverse_sentence(test_string) - test_string.must_equal "awesome! is Yoda" + _(test_string).must_equal "awesome! is Yoda" end end @@ -27,7 +27,7 @@ reverse_sentence(test_string) - test_string.must_be_empty + _(test_string).must_be_empty end # if the parameter is an object, check for nil @@ -36,7 +36,7 @@ reverse_sentence(test_string) - test_string.must_be_nil + _(test_string).must_be_nil end it "reverse a sentence with one word" do @@ -44,7 +44,7 @@ reverse_sentence(test_string) - test_string.must_equal "world" + _(test_string).must_equal "world" end it "reverse a sentence with multiple words" do @@ -52,7 +52,7 @@ reverse_sentence(test_string) - test_string.must_equal "yesterday. was I than today engineer better a I'm" + _(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 @@ -60,15 +60,15 @@ reverse_sentence(test_string) - test_string.must_equal "apples? them like you do How" + _(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 " + _(test_string).must_equal " this! do can I " end end end From 1bccef62c9e06f7ef2e9e020366d4066ed59b9ce Mon Sep 17 00:00:00 2001 From: Farah Davoodi Date: Sun, 29 Sep 2019 18:45:05 -0700 Subject: [PATCH 5/6] cleaned up string split but unable to pass trailing whitespace test... --- 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 1180349..a919ea1 100644 --- a/lib/reverse_sentence.rb +++ b/lib/reverse_sentence.rb @@ -8,7 +8,7 @@ def reverse_sentence(my_sentence) end #Take in string and convert to array of words with spaces perseved - sentence_array = my_sentence.split(/[\s]/) + sentence_array = my_sentence.split(/\s/) #Iterate through array swapping each words i = 0 From fd7c56eedf77e9a399923e4eb9fc8e6500c9757f Mon Sep 17 00:00:00 2001 From: Farah Davoodi Date: Sun, 6 Oct 2019 15:30:56 -0700 Subject: [PATCH 6/6] Reviewed problem with tutor and re-worked reverse_sentence problem --- lib/reverse_sentence.rb | 95 ++++++++++++++++++++++++++++++++--------- 1 file changed, 74 insertions(+), 21 deletions(-) diff --git a/lib/reverse_sentence.rb b/lib/reverse_sentence.rb index a919ea1..784338c 100644 --- a/lib/reverse_sentence.rb +++ b/lib/reverse_sentence.rb @@ -1,34 +1,87 @@ -require 'pry' # A method to reverse the words in a sentence, in place. -# Time complexity: ? -# Space complexity: ? + +# Time complexity: O(n^2) Where n is the length of the string, my_sentence. We iterate through the entire sentence but when a "word" is reached we iterate through that word, swapping each letter. +# Space complexity: O(1) because nothing new is created, everything is swapped/reversed in place. + def reverse_sentence(my_sentence) if my_sentence == nil return nil end - #Take in string and convert to array of words with spaces perseved - sentence_array = my_sentence.split(/\s/) + # #Take in string and convert to array of words with spaces perseved + # sentence_array = my_sentence.split(/\s/) - #Iterate through array swapping each words - i = 0 - j = sentence_array.length - 1 - while i < j - temp_index = sentence_array[i] - sentence_array[i] = sentence_array[j] - sentence_array[j] = temp_index + # #Iterate through array swapping each words + # i = 0 + # j = sentence_array.length - 1 + # while i < j + # temp_index = sentence_array[i] + # sentence_array[i] = sentence_array[j] + # sentence_array[j] = temp_index - i += 1 - j -= 1 - end + # i += 1 + # j -= 1 + # end - #Concatente elements of array into a new string output using (.join) - new_sentence = sentence_array.join(' ') + # #Concatente elements of array into a new string output using (.join) + # new_sentence = sentence_array.join(' ') - #iterate through new string and replace each character into parameter, my_sentence - new_sentence.length.times do |i| - my_sentence[i] = new_sentence[i] - end + # #iterate through new string and replace each character into parameter, my_sentence + # new_sentence.length.times do |i| + # my_sentence[i] = new_sentence[i] + # end + # return my_sentence + + ############ Second Try at Problem ############# + + # Step 1 - Reverse sentence in its entirity + # Create helper method to do this + def reverse(string) + start_point = 0 + end_point = string.length - 1 + while start_point < end_point + temp = string[start_point] + string[start_point] = string[end_point] + string[end_point] = temp + start_point += 1 + end_point -= 1 + end + return string + end + + # Pass in my_sentence into Step 1 helper method + reverse(my_sentence) + + # Step 2 Identify 'words' in sentence + # Identify start and end positions of each word + i = 0 + start_point = 0 + while i < my_sentence.length + if my_sentence[i] == " " + end_point = i - 1 + + while start_point < end_point + temp = my_sentence[start_point] + my_sentence[start_point] = my_sentence[end_point] + my_sentence[end_point] = temp + start_point += 1 + end_point -= 1 + end + start_point = i + 1 + + elsif i == my_sentence.length - 1 + end_point = i + while start_point < end_point + temp = my_sentence[start_point] + my_sentence[start_point] = my_sentence[end_point] + my_sentence[end_point] = temp + start_point += 1 + end_point -= 1 + end + + end + i += 1 + end return my_sentence end