From a093f2c7cc2398ad76c4cf51a932e0913045aacf Mon Sep 17 00:00:00 2001 From: Rinostar Date: Fri, 27 Sep 2019 12:31:35 -0700 Subject: [PATCH 1/9] Finished wave_1 --- lib/sort_by_length.rb | 27 +++++++++++++++---- ...rt_by_length.rb => sort_by_length_test.rb} | 0 2 files changed, 22 insertions(+), 5 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..c32b985 100644 --- a/lib/sort_by_length.rb +++ b/lib/sort_by_length.rb @@ -1,7 +1,24 @@ # A method which will return an array of the words in the string -# sorted by the length of the word. -# Time complexity: ? -# Space complexity: ? +# sorted by the length of the word. +# Time complexity: O(n2) +# Space complexity: O(n) + def sort_by_length(my_sentence) - raise NotImplementedError, "Method not implemented" -end + #Step_1: break the sentence into an array of words + my_array = my_sentence.split(/\W+/) + #Step_2: compare the words and sort the array + i = 0 + while i < my_array.length-1 + j = 0 + while j < my_array.length-i-1 + if my_array[j].length > my_array[j+1].length + temp = my_array[j] + my_array[j] = my_array[j+1] + my_array[j+1] = temp + end + j += 1 + end + i += 1 + end + return my_array +end \ No newline at end of file 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 From 5db16ea3938a70efa4b57769fb8449431d4a9b75 Mon Sep 17 00:00:00 2001 From: Rinostar Date: Sat, 28 Sep 2019 16:53:54 -0700 Subject: [PATCH 2/9] 1/2 reverse_sentence.rb --- lib/reverse_sentence.rb | 48 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 4 deletions(-) diff --git a/lib/reverse_sentence.rb b/lib/reverse_sentence.rb index 3fe7cac..e93e840 100644 --- a/lib/reverse_sentence.rb +++ b/lib/reverse_sentence.rb @@ -1,6 +1,46 @@ # A method to reverse the words in a sentence, in place. -# Time complexity: ? -# Space complexity: ? -def reverse_sentence(my_sentence) - raise NotImplementedError +# Time complexity: O(n2) +# Space complexity: O(n) +def reverse_sentence(my_sentence) + #Step_1: Reverse all characters within the string + if my_sentence.length % 2 == 0 + pairs = my_sentence.length / 2 + else + pairs = (my_sentence.length-1) / 2 + end + + i = 1 + (pairs).times do + temp = my_sentence[i-1] + my_sentence[i-1] = my_sentence[-i] + my_sentence[-i] = temp + i += 1 + end + + #Step_2: Reverse letters for each words + space_indexes = (0...my_sentence.length).find_all { |c| my_sentence[c] == " " } + + # a is a tracker to loop through space_indexes array + a = 0 + (space_indexes.length).times do + if space_indexes[a] % 2 == 0 + pairs = space_indexes[a] / 2 + else + pairs = (space_indexes[a]-1) / 2 + end + + # b is a tracker to reverse all letters for the current word + b = 1 + (pairs).times do + temp = my_sentence[b-1] + my_sentence[b-1] = my_sentence[space_indexes[a]-i] + my_sentence[space_indexes[a]-i] = temp + b += 1 + end + a += 1 + end + end + +test = "Hello world!" +result = reverse_sentence(test) \ No newline at end of file From 0910b51758422f0fe18c2b96c744675c51b54166 Mon Sep 17 00:00:00 2001 From: Rinostar Date: Sat, 28 Sep 2019 17:54:29 -0700 Subject: [PATCH 3/9] wave_2 (rough draft) --- Gemfile.lock | 30 +++++++++++++ lib/reverse_sentence.rb | 99 ++++++++++++++++++++++++++++++----------- 2 files changed, 102 insertions(+), 27 deletions(-) create mode 100644 Gemfile.lock diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..cae865a --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,30 @@ +GEM + remote: http://rubygems.org/ + specs: + ansi (1.5.0) + builder (3.2.3) + minitest (5.10.3) + minitest-reporters (1.3.8) + ansi + builder + minitest (>= 5.0) + ruby-progressbar + minitest-skip (0.0.1) + minitest (~> 5.0) + rake (12.3.0) + ruby-progressbar (1.10.1) + +PLATFORMS + ruby + +DEPENDENCIES + minitest + minitest-reporters + minitest-skip + rake + +RUBY VERSION + ruby 2.5.1p57 + +BUNDLED WITH + 1.17.3 diff --git a/lib/reverse_sentence.rb b/lib/reverse_sentence.rb index e93e840..ee9be78 100644 --- a/lib/reverse_sentence.rb +++ b/lib/reverse_sentence.rb @@ -2,45 +2,90 @@ # Time complexity: O(n2) # Space complexity: O(n) def reverse_sentence(my_sentence) - #Step_1: Reverse all characters within the string - if my_sentence.length % 2 == 0 - pairs = my_sentence.length / 2 - else - pairs = (my_sentence.length-1) / 2 + #Step_0: Screen out all sentences that doesn't need to be/can't be reversed + if my_sentence == nil || my_sentence == "" + return my_sentence + exit + elsif my_sentence.include?("\s") == false + return my_sentence + exit end + #Step_1: Reverse all characters within the string in place + sentence_pairs = paris_helper(my_sentence.length) i = 1 - (pairs).times do + (sentence_pairs).times do temp = my_sentence[i-1] my_sentence[i-1] = my_sentence[-i] my_sentence[-i] = temp i += 1 end - #Step_2: Reverse letters for each words + #Step_2: Find all indexes for the whitespaces within the current string space_indexes = (0...my_sentence.length).find_all { |c| my_sentence[c] == " " } - # a is a tracker to loop through space_indexes array - a = 0 - (space_indexes.length).times do - if space_indexes[a] % 2 == 0 - pairs = space_indexes[a] / 2 - else - pairs = (space_indexes[a]-1) / 2 - end - - # b is a tracker to reverse all letters for the current word - b = 1 - (pairs).times do - temp = my_sentence[b-1] - my_sentence[b-1] = my_sentence[space_indexes[a]-i] - my_sentence[space_indexes[a]-i] = temp - b += 1 - end - a += 1 + #Step_3: Reverse all characters for each word within the string in place + if space_indexes.length == 1 + my_sentence.reverse_first + my_sentence.reverse_last + else + my_sentence.reverse_first + my_sentence.reverse_last + my_sentence.reverse_middle + end + + return my_sentence +end + +# Helper methoder to determin pairs of character that needs to be swapped +def paris_helper(length) + if my_sentence.length % 2 == 0 + pairs = my_sentence.length / 2 + else + pairs = (my_sentence.length-1) / 2 + end + return pairs +end + +# Helpfer method to reverse the first word in the string +def reverse_first + word_pairs = paris_helper(space_indexes[0]) + i = 1 + (word_pairs).times do + temp = my_sentence[i-1] + my_sentence[i-1] = my_sentence[space_indexes[0]-i] + my_sentence[space_indexes[0]-i] = temp + i += 1 end +end +# Helpfer method to reverse the last word in the string +def reverse_last + word_pairs = paris_helper(space_indexes[-1]) + i = 1 + (word_pairs).times do + temp = my_sentence[-i] + my_sentence[-i] = my_sentence[space_indexes[-1]+i] + my_sentence[space_indexes[-1]+i] = temp + i += 1 + end end -test = "Hello world!" -result = reverse_sentence(test) \ No newline at end of file +# Helper method to reverse the middle parts of the string (word(s) between the first and last words) +def reverse_middle + until tracker == space_indexes[-1] + i = 0 + tracker == space_indexes[i] + if my_sentence[space_indexes[i]+1] != " " + word_pairs = paris_helper(space_indexes[i+1]-space_indexes[i]-1) + n = 1 + (word_pairs).times do + temp = my_sentence[space_indexes[i]+n] + my_sentence[space_indexes[i]+n] = my_sentence[space_indexes[i+1]-n] + my_sentence[space_indexes[i+1]-n] = temp + n += 1 + end + end + i += 1 + end +end \ No newline at end of file From cefb578ba9f82b05a8c8a4c25b87981fcd22e905 Mon Sep 17 00:00:00 2001 From: Rinostar Date: Sat, 28 Sep 2019 21:07:29 -0700 Subject: [PATCH 4/9] wave_2 finalized --- lib/reverse_sentence.rb | 93 +++++++++++++++++------------------------ 1 file changed, 39 insertions(+), 54 deletions(-) diff --git a/lib/reverse_sentence.rb b/lib/reverse_sentence.rb index ee9be78..c09a412 100644 --- a/lib/reverse_sentence.rb +++ b/lib/reverse_sentence.rb @@ -3,16 +3,13 @@ # Space complexity: O(n) def reverse_sentence(my_sentence) #Step_0: Screen out all sentences that doesn't need to be/can't be reversed - if my_sentence == nil || my_sentence == "" - return my_sentence - exit - elsif my_sentence.include?("\s") == false + if my_sentence == nil || my_sentence == "" || my_sentence.include?("\s") == false return my_sentence exit end #Step_1: Reverse all characters within the string in place - sentence_pairs = paris_helper(my_sentence.length) + sentence_pairs = pairs_helper(my_sentence.length) i = 1 (sentence_pairs).times do temp = my_sentence[i-1] @@ -21,35 +18,12 @@ def reverse_sentence(my_sentence) i += 1 end - #Step_2: Find all indexes for the whitespaces within the current string + #Step_2: Find all whitespace index(es) within the current string space_indexes = (0...my_sentence.length).find_all { |c| my_sentence[c] == " " } - #Step_3: Reverse all characters for each word within the string in place - if space_indexes.length == 1 - my_sentence.reverse_first - my_sentence.reverse_last - else - my_sentence.reverse_first - my_sentence.reverse_last - my_sentence.reverse_middle - end - - return my_sentence -end - -# Helper methoder to determin pairs of character that needs to be swapped -def paris_helper(length) - if my_sentence.length % 2 == 0 - pairs = my_sentence.length / 2 - else - pairs = (my_sentence.length-1) / 2 - end - return pairs -end - -# Helpfer method to reverse the first word in the string -def reverse_first - word_pairs = paris_helper(space_indexes[0]) + #Step_3: Reverse all characters for each word based on the whitespace index(es) + #Step_3.1: Reverse the first word in the string + word_pairs = pairs_helper(space_indexes[0]) i = 1 (word_pairs).times do temp = my_sentence[i-1] @@ -57,11 +31,8 @@ def reverse_first my_sentence[space_indexes[0]-i] = temp i += 1 end -end - -# Helpfer method to reverse the last word in the string -def reverse_last - word_pairs = paris_helper(space_indexes[-1]) + #Step_3.2: reverse the last word in the string + word_pairs = pairs_helper((my_sentence.length - 1) - space_indexes[-1]) i = 1 (word_pairs).times do temp = my_sentence[-i] @@ -69,23 +40,37 @@ def reverse_last my_sentence[space_indexes[-1]+i] = temp i += 1 end -end - -# Helper method to reverse the middle parts of the string (word(s) between the first and last words) -def reverse_middle - until tracker == space_indexes[-1] - i = 0 - tracker == space_indexes[i] - if my_sentence[space_indexes[i]+1] != " " - word_pairs = paris_helper(space_indexes[i+1]-space_indexes[i]-1) - n = 1 - (word_pairs).times do - temp = my_sentence[space_indexes[i]+n] - my_sentence[space_indexes[i]+n] = my_sentence[space_indexes[i+1]-n] - my_sentence[space_indexes[i+1]-n] = temp - n += 1 + #Step_3.3: reverse the words in between (if any) + i = 0 + if space_indexes.length == 1 + return my_sentence + else + until i == (space_indexes.length - 1) + if my_sentence[space_indexes[i]+1] != " " + word_pairs = pairs_helper(space_indexes[i+1]-space_indexes[i]-1) + n = 1 + (word_pairs).times do + temp = my_sentence[space_indexes[i]+n] + my_sentence[space_indexes[i]+n] = my_sentence[space_indexes[i+1]-n] + my_sentence[space_indexes[i+1]-n] = temp + n += 1 + end end + i += 1 end - i += 1 + return my_sentence end -end \ No newline at end of file +end + +# Helper methoder to determin pairs of character that needs to be swapped +def pairs_helper(length) + if length % 2 == 0 + pairs = length / 2 + else + pairs = (length - 1) / 2 + end + return pairs +end + +# test_string = "Yoda is awesome!" +# test_result = reverse_sentence(test_string) \ No newline at end of file From 1eea754115d91660486385d4b7c687b8e1dbe7b5 Mon Sep 17 00:00:00 2001 From: Rinostar Date: Sat, 28 Sep 2019 21:09:58 -0700 Subject: [PATCH 5/9] formatting --- lib/reverse_sentence.rb | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/reverse_sentence.rb b/lib/reverse_sentence.rb index c09a412..6486098 100644 --- a/lib/reverse_sentence.rb +++ b/lib/reverse_sentence.rb @@ -62,7 +62,7 @@ def reverse_sentence(my_sentence) end end -# Helper methoder to determin pairs of character that needs to be swapped +# Helper method to determin pairs of character that needs to be swapped def pairs_helper(length) if length % 2 == 0 pairs = length / 2 @@ -70,7 +70,4 @@ def pairs_helper(length) pairs = (length - 1) / 2 end return pairs -end - -# test_string = "Yoda is awesome!" -# test_result = reverse_sentence(test_string) \ No newline at end of file +end \ No newline at end of file From c29ac413b5c0a11b409241e1f88f3ffcc3f2ad36 Mon Sep 17 00:00:00 2001 From: Rinostar Date: Wed, 27 Nov 2019 15:30:06 -0800 Subject: [PATCH 6/9] Refactor based on feedback: take off helper method --- lib/reverse_sentence.rb | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/lib/reverse_sentence.rb b/lib/reverse_sentence.rb index 6486098..9904221 100644 --- a/lib/reverse_sentence.rb +++ b/lib/reverse_sentence.rb @@ -9,7 +9,7 @@ def reverse_sentence(my_sentence) end #Step_1: Reverse all characters within the string in place - sentence_pairs = pairs_helper(my_sentence.length) + sentence_pairs = my_sentence.length/2 i = 1 (sentence_pairs).times do temp = my_sentence[i-1] @@ -23,7 +23,7 @@ def reverse_sentence(my_sentence) #Step_3: Reverse all characters for each word based on the whitespace index(es) #Step_3.1: Reverse the first word in the string - word_pairs = pairs_helper(space_indexes[0]) + word_pairs = space_indexes[0]/2 i = 1 (word_pairs).times do temp = my_sentence[i-1] @@ -32,7 +32,7 @@ def reverse_sentence(my_sentence) i += 1 end #Step_3.2: reverse the last word in the string - word_pairs = pairs_helper((my_sentence.length - 1) - space_indexes[-1]) + word_pairs = ((my_sentence.length - 1) - space_indexes[-1])/2 i = 1 (word_pairs).times do temp = my_sentence[-i] @@ -47,7 +47,7 @@ def reverse_sentence(my_sentence) else until i == (space_indexes.length - 1) if my_sentence[space_indexes[i]+1] != " " - word_pairs = pairs_helper(space_indexes[i+1]-space_indexes[i]-1) + word_pairs = (space_indexes[i+1]-space_indexes[i]-1)/2 n = 1 (word_pairs).times do temp = my_sentence[space_indexes[i]+n] @@ -60,14 +60,4 @@ def reverse_sentence(my_sentence) end return my_sentence end -end - -# Helper method to determin pairs of character that needs to be swapped -def pairs_helper(length) - if length % 2 == 0 - pairs = length / 2 - else - pairs = (length - 1) / 2 - end - return pairs end \ No newline at end of file From 37d9a197ad6ef9d48f497c59ca5661945bf7d082 Mon Sep 17 00:00:00 2001 From: Rinostar Date: Wed, 27 Nov 2019 19:58:14 -0800 Subject: [PATCH 7/9] Refactor_2: add reverse_helper method --- lib/reverse_sentence.rb | 82 +++++++++++++++-------------------------- 1 file changed, 29 insertions(+), 53 deletions(-) diff --git a/lib/reverse_sentence.rb b/lib/reverse_sentence.rb index 9904221..80b174c 100644 --- a/lib/reverse_sentence.rb +++ b/lib/reverse_sentence.rb @@ -1,63 +1,39 @@ # A method to reverse the words in a sentence, in place. # Time complexity: O(n2) # Space complexity: O(n) -def reverse_sentence(my_sentence) - #Step_0: Screen out all sentences that doesn't need to be/can't be reversed - if my_sentence == nil || my_sentence == "" || my_sentence.include?("\s") == false - return my_sentence - exit +def reverse_helper(string, s, e) + pairs = (e + 1 - s)/2 + pairs.times do + temp = string[s] + string[s] = string[e] + string[e] = temp + s += 1 + e -= 1 end +end - #Step_1: Reverse all characters within the string in place - sentence_pairs = my_sentence.length/2 - i = 1 - (sentence_pairs).times do - temp = my_sentence[i-1] - my_sentence[i-1] = my_sentence[-i] - my_sentence[-i] = temp - i += 1 - end - - #Step_2: Find all whitespace index(es) within the current string - space_indexes = (0...my_sentence.length).find_all { |c| my_sentence[c] == " " } +def reverse_sentence(sentence) + return sentence if sentence == nil || sentence == "" || sentence.include?("\s") == false + reverse_helper(sentence, 0, (sentence.length - 1)) - #Step_3: Reverse all characters for each word based on the whitespace index(es) - #Step_3.1: Reverse the first word in the string - word_pairs = space_indexes[0]/2 - i = 1 - (word_pairs).times do - temp = my_sentence[i-1] - my_sentence[i-1] = my_sentence[space_indexes[0]-i] - my_sentence[space_indexes[0]-i] = temp - i += 1 - end - #Step_3.2: reverse the last word in the string - word_pairs = ((my_sentence.length - 1) - space_indexes[-1])/2 - i = 1 - (word_pairs).times do - temp = my_sentence[-i] - my_sentence[-i] = my_sentence[space_indexes[-1]+i] - my_sentence[space_indexes[-1]+i] = temp - i += 1 - end - #Step_3.3: reverse the words in between (if any) - i = 0 - if space_indexes.length == 1 - return my_sentence - else - until i == (space_indexes.length - 1) - if my_sentence[space_indexes[i]+1] != " " - word_pairs = (space_indexes[i+1]-space_indexes[i]-1)/2 - n = 1 - (word_pairs).times do - temp = my_sentence[space_indexes[i]+n] - my_sentence[space_indexes[i]+n] = my_sentence[space_indexes[i+1]-n] - my_sentence[space_indexes[i+1]-n] = temp - n += 1 - end + j = 0 + spaces = [] + while j <= sentence.length + if sentence[j] == " " + spaces << j + if spaces.length == 1 + # reverse the first word + reverse_helper(sentence, 0, (j - 1)) + elsif spaces.length > 1 + # reverse the middle word + reverse_helper(sentence, (spaces[-2] + 1), (j - 1)) end - i += 1 + elsif sentence[j] == nil + # reverse the last word + reverse_helper(sentence, (spaces[-1] + 1), (j - 1)) end - return my_sentence + j += 1 end + + return sentence end \ No newline at end of file From 297c8063ed0ea1fc0ac7b98b355316c22ec89e09 Mon Sep 17 00:00:00 2001 From: Rinostar Date: Wed, 27 Nov 2019 20:05:21 -0800 Subject: [PATCH 8/9] update big(o) for reverse_sentence --- lib/reverse_sentence.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/reverse_sentence.rb b/lib/reverse_sentence.rb index 80b174c..0b61cc7 100644 --- a/lib/reverse_sentence.rb +++ b/lib/reverse_sentence.rb @@ -1,6 +1,6 @@ # A method to reverse the words in a sentence, in place. -# Time complexity: O(n2) -# Space complexity: O(n) +# Time complexity: O(n*m)??? +# Space complexity: O(1) def reverse_helper(string, s, e) pairs = (e + 1 - s)/2 pairs.times do @@ -34,6 +34,6 @@ def reverse_sentence(sentence) end j += 1 end - + return sentence end \ No newline at end of file From 6aebf8204ba3e348ef632ab92073fb91b8affe39 Mon Sep 17 00:00:00 2001 From: Rinostar Date: Wed, 27 Nov 2019 20:23:21 -0800 Subject: [PATCH 9/9] Refactor_3: use insertion sort for sort_by_length method --- lib/sort_by_length.rb | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/lib/sort_by_length.rb b/lib/sort_by_length.rb index c32b985..ee7a722 100644 --- a/lib/sort_by_length.rb +++ b/lib/sort_by_length.rb @@ -3,22 +3,21 @@ # Time complexity: O(n2) # Space complexity: O(n) -def sort_by_length(my_sentence) +def sort_by_length(sentence) #Step_1: break the sentence into an array of words - my_array = my_sentence.split(/\W+/) - #Step_2: compare the words and sort the array - i = 0 - while i < my_array.length-1 - j = 0 - while j < my_array.length-i-1 - if my_array[j].length > my_array[j+1].length - temp = my_array[j] - my_array[j] = my_array[j+1] - my_array[j+1] = temp - end - j += 1 + array = sentence.split(/\W+/) + #Step_2: use insertion sort to sort the array + i = 1 + while i < array.length + to_insert = array[i] + j = i + while j > 0 && array[j-1].length > to_insert.length + array[j] = array[j-1] + j -= 1 end + array[j] = to_insert i += 1 end - return my_array + + return array end \ No newline at end of file