From 2f64657daeaac114a32b6139033892b834927cdf Mon Sep 17 00:00:00 2001 From: Julia A Kingrey Date: Sun, 22 Sep 2019 15:52:48 -0700 Subject: [PATCH 1/6] Oops -- included the updated file this time. --- lib/sort_by_length.rb | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/lib/sort_by_length.rb b/lib/sort_by_length.rb index a5713ad..132a755 100644 --- a/lib/sort_by_length.rb +++ b/lib/sort_by_length.rb @@ -1,7 +1,33 @@ # 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: This is an insertion sort with two loops, so time complexity is O(n^2) / quadratic +# Space complexity: O(1) / constant + +require "pry" + +"I", "love", "great", "awesome", "words" def sort_by_length(my_sentence) - raise NotImplementedError, "Method not implemented" + # split sentence into array of strings + my_sentence_words = my_sentence.split(" ") + array_length = my_sentence_words.length + i = 1 + # set condition for outer loop to run + while i < array_length + # declare value for word to be considered + to_insert = my_sentence_words[i] # to_insert = "words" + j = i # j = 4 + # set conditions for inner loop to run + while j > 0 && my_sentence_words[j-1].length > to_insert.length + # if conditions evaluate true, move word at [j] down one position + my_sentence_words[j] = my_sentence_words[j-1] + # decrement counter for inner loop + j -= 1 + end + # declare value + my_sentence_words[j] = to_insert # "words" + # increment counter for outer loop + i += 1 + end + return my_sentence_words end + From 482473454add6e888b8e35e9c91870717def35cd Mon Sep 17 00:00:00 2001 From: Julia A Kingrey Date: Sun, 22 Sep 2019 15:57:55 -0700 Subject: [PATCH 2/6] Added some notes to sort_by_length --- lib/sort_by_length.rb | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/sort_by_length.rb b/lib/sort_by_length.rb index 132a755..c5aeb82 100644 --- a/lib/sort_by_length.rb +++ b/lib/sort_by_length.rb @@ -3,8 +3,6 @@ # Time complexity: This is an insertion sort with two loops, so time complexity is O(n^2) / quadratic # Space complexity: O(1) / constant -require "pry" - "I", "love", "great", "awesome", "words" def sort_by_length(my_sentence) # split sentence into array of strings @@ -14,7 +12,7 @@ def sort_by_length(my_sentence) # set condition for outer loop to run while i < array_length # declare value for word to be considered - to_insert = my_sentence_words[i] # to_insert = "words" + to_insert = my_sentence_words[i] j = i # j = 4 # set conditions for inner loop to run while j > 0 && my_sentence_words[j-1].length > to_insert.length @@ -23,8 +21,8 @@ def sort_by_length(my_sentence) # decrement counter for inner loop j -= 1 end - # declare value - my_sentence_words[j] = to_insert # "words" + # not 100 percent sure what this variable is doing but I know it's necessary + my_sentence_words[j] = to_insert # increment counter for outer loop i += 1 end From 7cdcd16715dc3d676086ce586f21ba19f921b7b7 Mon Sep 17 00:00:00 2001 From: Julia A Kingrey Date: Sun, 22 Sep 2019 17:33:39 -0700 Subject: [PATCH 3/6] Strange -- reverse_sentence works perfectly in irb but failing most tests. Checked path relatives. --- lib/reverse_sentence.rb | 18 +++++++++++++++++- lib/sort_by_length.rb | 1 - 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/reverse_sentence.rb b/lib/reverse_sentence.rb index 3fe7cac..5803568 100644 --- a/lib/reverse_sentence.rb +++ b/lib/reverse_sentence.rb @@ -1,6 +1,22 @@ +require "pry" + # A method to reverse the words in a sentence, in place. # Time complexity: ? # Space complexity: ? def reverse_sentence(my_sentence) - raise NotImplementedError + my_sentence_words = my_sentence.split + i = 0 + j = i - 1 + # loop through elements n/2 times, ignore central element if length is odd + while i < (my_sentence_words.length) / 2 + # swap the elements at i and j + my_sentence_words[i], my_sentence_words[j] = my_sentence_words[j], my_sentence_words[i] + # increment i + i += 1 + # decrement j + j -= 1 + end + reversed_sentence = my_sentence_words.join(" ") + + return reversed_sentence end diff --git a/lib/sort_by_length.rb b/lib/sort_by_length.rb index c5aeb82..d3c617b 100644 --- a/lib/sort_by_length.rb +++ b/lib/sort_by_length.rb @@ -3,7 +3,6 @@ # Time complexity: This is an insertion sort with two loops, so time complexity is O(n^2) / quadratic # Space complexity: O(1) / constant -"I", "love", "great", "awesome", "words" def sort_by_length(my_sentence) # split sentence into array of strings my_sentence_words = my_sentence.split(" ") From d58dfcfb170ea63e43a52aa2cce2358e233975f6 Mon Sep 17 00:00:00 2001 From: Julia A Kingrey Date: Sun, 22 Sep 2019 18:15:13 -0700 Subject: [PATCH 4/6] Refined reverse_sentence code so that it passes all tests ... in irb. Still only passing three tests in Ruby. --- lib/reverse_sentence.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/reverse_sentence.rb b/lib/reverse_sentence.rb index 5803568..6765d70 100644 --- a/lib/reverse_sentence.rb +++ b/lib/reverse_sentence.rb @@ -1,13 +1,13 @@ -require "pry" # A method to reverse the words in a sentence, in place. # Time complexity: ? # Space complexity: ? + def reverse_sentence(my_sentence) - my_sentence_words = my_sentence.split + my_sentence_words = my_sentence.split(/\s/) i = 0 j = i - 1 - # loop through elements n/2 times, ignore central element if length is odd + # loop through elements n/2 times (ignores central element if length is odd) while i < (my_sentence_words.length) / 2 # swap the elements at i and j my_sentence_words[i], my_sentence_words[j] = my_sentence_words[j], my_sentence_words[i] @@ -17,6 +17,5 @@ def reverse_sentence(my_sentence) j -= 1 end reversed_sentence = my_sentence_words.join(" ") - return reversed_sentence end From 1c49c1f456469788f5cbf05908cd8df2f260dbe0 Mon Sep 17 00:00:00 2001 From: Julia A Kingrey Date: Mon, 23 Sep 2019 13:32:54 -0700 Subject: [PATCH 5/6] Throwing in the towel after 4+ hours. I can reverse everything in the sentence (Yoda => adoY) but I can't figure out how to un-reverse words post transformation. --- lib/reverse_sentence.rb | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/lib/reverse_sentence.rb b/lib/reverse_sentence.rb index 6765d70..6db9bdc 100644 --- a/lib/reverse_sentence.rb +++ b/lib/reverse_sentence.rb @@ -3,19 +3,15 @@ # Time complexity: ? # Space complexity: ? -def reverse_sentence(my_sentence) - my_sentence_words = my_sentence.split(/\s/) - i = 0 - j = i - 1 +def reverse_sentence(test_string) + half = test_string.length / 2 # loop through elements n/2 times (ignores central element if length is odd) - while i < (my_sentence_words.length) / 2 - # swap the elements at i and j - my_sentence_words[i], my_sentence_words[j] = my_sentence_words[j], my_sentence_words[i] - # increment i - i += 1 - # decrement j - j -= 1 + half.times do |i| + # swap all characters/whitespace (" Yoda" => "adoY ") + test_string[i], test_string[-i-1] = test_string[-i-1], test_string[i] end - reversed_sentence = my_sentence_words.join(" ") - return reversed_sentence + + # can't figure out how to reverse letters in each word :( + + return test_string end From 31faec67ec312673edbf815acb7a638e5502c017 Mon Sep 17 00:00:00 2001 From: Julia A Kingrey Date: Thu, 3 Oct 2019 10:57:34 -0700 Subject: [PATCH 6/6] Adding comment from in-class discussion --- lib/reverse_sentence.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/reverse_sentence.rb b/lib/reverse_sentence.rb index 6db9bdc..72f5fc8 100644 --- a/lib/reverse_sentence.rb +++ b/lib/reverse_sentence.rb @@ -15,3 +15,5 @@ def reverse_sentence(test_string) return test_string end + +# per Dianna, I can reverse words in a new array and then reassign values to original array. Does this count as modifying in place? \ No newline at end of file