From 06c3d2ef2397a39065dfe9fe856030b19b2f00b0 Mon Sep 17 00:00:00 2001 From: Raisah Vesteinsdottir Date: Tue, 24 Sep 2019 15:05:25 -0700 Subject: [PATCH 1/3] Completed sort_by_length method --- lib/reverse_sentence.rb | 97 ++++++++++++++++++++++++++++++++++++++++- lib/sort_by_length.rb | 25 ++++++++++- 2 files changed, 120 insertions(+), 2 deletions(-) diff --git a/lib/reverse_sentence.rb b/lib/reverse_sentence.rb index 3fe7cac..b265ba2 100644 --- a/lib/reverse_sentence.rb +++ b/lib/reverse_sentence.rb @@ -2,5 +2,100 @@ # Time complexity: ? # Space complexity: ? def reverse_sentence(my_sentence) - raise NotImplementedError + i = 0 + j = my_sentence.length - 1 + + while i < (my_sentence.length / 2) + temp_a = my_sentence[i] + temp_b = my_sentence[j] + + my_sentence[i] = temp_b + my_sentence[j] = temp_a + + i += 1 + j -= 1 + end + + x = 0 + word_start = 0 + word_end = 0 + in_word = true + + while x < my_sentence.length + if (my_sentence[x] == " " || x == (my_sentence.length - 1) )&& in_word == true + if my_sentence[x] == " " + word_end = x - 1 + elsif x == (my_sentence.length - 1) + word_end = x + end + + word_length = (word_end - word_start + 1) + puts word_length + + y = word_start + z = word_end + count = 0 + + + while count < (word_length / 2) + temp_char_one = my_sentence[y] + temp_char_two = my_sentence[z] + + my_sentence[y] = temp_char_two + my_sentence[z] = temp_char_one + + y += 1 + z -= 1 + count += 1 + end + + in_word = false + end + + if my_sentence[x] != " " && in_word == false + word_start = x + in_word = true + end + + x += 1 + end + + + # until my_sentence[i] == " " + # i += 1 + # end + # first_word = my_sentence[0, i] + + + # j = my_sentence.length - 1 + # until my_sentence[j] == " " + # j -= 1 + # end + # last_word = my_sentence[j + 1, my_sentence.length - 1] + + # my_sentence.insert(0, my_sentence[j + 1, my_sentence.length - 1]) + # my_sentence.delete!("better") + + puts word_start + puts word_end + + return my_sentence end + +puts reverse_sentence("I'm an animal") + + + +# words = [] +# start = 0 + +# while i < my_sentence.length +# if my_sentence[i] == " " || i == my_sentence.length - 1 +# finish = i +# words << my_sentence[start, finish] +# start = i +# end + +# i += 1 + +# end \ No newline at end of file diff --git a/lib/sort_by_length.rb b/lib/sort_by_length.rb index a5713ad..e756006 100644 --- a/lib/sort_by_length.rb +++ b/lib/sort_by_length.rb @@ -3,5 +3,28 @@ # Time complexity: ? # Space complexity: ? def sort_by_length(my_sentence) - raise NotImplementedError, "Method not implemented" + my_sentence_array = my_sentence.split(" ") + + i = 1 + while i < my_sentence_array.length + value = i + j = i - 1 + i.times do + if my_sentence_array[value].length < my_sentence_array[j].length + new_value = my_sentence_array[j] + new_j = my_sentence_array[value] + + my_sentence_array[value] = new_value + my_sentence_array[j] = new_j + end + j -= 1 + value -= 1 + end + i += 1 + end + + return my_sentence_array end + + +puts sort_by_length("words are the a") \ No newline at end of file From e210cbc89850332854655dc1415548fc25584c55 Mon Sep 17 00:00:00 2001 From: Raisah Vesteinsdottir Date: Tue, 24 Sep 2019 15:17:16 -0700 Subject: [PATCH 2/3] Started reverse_sentence method --- lib/reverse_sentence.rb | 96 +++++++++++++---------------------------- 1 file changed, 29 insertions(+), 67 deletions(-) diff --git a/lib/reverse_sentence.rb b/lib/reverse_sentence.rb index b265ba2..c086793 100644 --- a/lib/reverse_sentence.rb +++ b/lib/reverse_sentence.rb @@ -1,101 +1,63 @@ # A method to reverse the words in a sentence, in place. -# Time complexity: ? -# Space complexity: ? +# Time compleixity: ? +# Space compleixity: ? def reverse_sentence(my_sentence) + + return nil if my_sentence == nil + + # reverses all characters in sentence i = 0 j = my_sentence.length - 1 - while i < (my_sentence.length / 2) - temp_a = my_sentence[i] - temp_b = my_sentence[j] + while i < my_sentence.length / 2 + temp_i = my_sentence[i] + temp_j = my_sentence[j] - my_sentence[i] = temp_b - my_sentence[j] = temp_a + my_sentence[i] = temp_j + my_sentence[j] = temp_i i += 1 j -= 1 end - x = 0 + + # reverses any words in the sentence + i = 0 word_start = 0 word_end = 0 in_word = true - while x < my_sentence.length - if (my_sentence[x] == " " || x == (my_sentence.length - 1) )&& in_word == true - if my_sentence[x] == " " - word_end = x - 1 - elsif x == (my_sentence.length - 1) - word_end = x - end + while i < my_sentence.length + if ( my_sentence[i] == " " || i == my_sentence.length - 1 ) && in_word == true - word_length = (word_end - word_start + 1) - puts word_length + word_end = i + word_end -= 1 if my_sentence[i] == " " - y = word_start - z = word_end + word_length = word_end - word_start + 1 count = 0 - - while count < (word_length / 2) - temp_char_one = my_sentence[y] - temp_char_two = my_sentence[z] + while count < word_length / 2 + temp_start = my_sentence[word_start] + temp_end = my_sentence[word_end] - my_sentence[y] = temp_char_two - my_sentence[z] = temp_char_one + my_sentence[word_start] = temp_end + my_sentence[word_end] = temp_start - y += 1 - z -= 1 + word_start += 1 + word_end -= 1 count += 1 end in_word = false end - if my_sentence[x] != " " && in_word == false - word_start = x + if my_sentence[i] != " " && in_word == false + word_start = i in_word = true end - x += 1 + i += 1 end - - # until my_sentence[i] == " " - # i += 1 - # end - # first_word = my_sentence[0, i] - - - # j = my_sentence.length - 1 - # until my_sentence[j] == " " - # j -= 1 - # end - # last_word = my_sentence[j + 1, my_sentence.length - 1] - - # my_sentence.insert(0, my_sentence[j + 1, my_sentence.length - 1]) - # my_sentence.delete!("better") - - puts word_start - puts word_end - return my_sentence end - -puts reverse_sentence("I'm an animal") - - - -# words = [] -# start = 0 - -# while i < my_sentence.length -# if my_sentence[i] == " " || i == my_sentence.length - 1 -# finish = i -# words << my_sentence[start, finish] -# start = i -# end - -# i += 1 - -# end \ No newline at end of file From 579b8d346b8625bfbb1d0ae2ff08ecbd87b4fed8 Mon Sep 17 00:00:00 2001 From: Raisah Vesteinsdottir Date: Tue, 24 Sep 2019 15:30:19 -0700 Subject: [PATCH 3/3] Completed reverse_sentence method and questions --- lib/reverse_sentence.rb | 6 ++++-- lib/sort_by_length.rb | 23 ++++++++++++----------- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/lib/reverse_sentence.rb b/lib/reverse_sentence.rb index c086793..6d07aed 100644 --- a/lib/reverse_sentence.rb +++ b/lib/reverse_sentence.rb @@ -1,6 +1,8 @@ # A method to reverse the words in a sentence, in place. -# Time compleixity: ? -# Space compleixity: ? +# Time complexity: The time complexity of this method is O(nm) where n is the length of my_sentence and m is the length of a word in my_sentence. The first while loop has a time complexity of O(1/2n) where n is the length of my_sentence. The second while loop has a time complexity of O(n)where n is the length of my_sentence and a nested while loop with a time complexity of (1/2m) where m is the length of the word within the sentence. So the total time complexity is O(3/2n * 1/2m) or O(nm). + +# Space complexity: The space complexity of this method is O(1) because the space that the variables take up stays constant regardless of the lengthe of the string. + def reverse_sentence(my_sentence) return nil if my_sentence == nil diff --git a/lib/sort_by_length.rb b/lib/sort_by_length.rb index e756006..2c14596 100644 --- a/lib/sort_by_length.rb +++ b/lib/sort_by_length.rb @@ -1,30 +1,31 @@ # 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: The time complexity of this method is O(n) where n is the length of the my_sentence_array, or the number of words in the my_sentence string. The time that the algorithm takes will increase linearly with the number of words passed in. + +# Space complexity: The space complexity of this method is O(1) because the space that the variables take up stays constant regardless of how many words the string contains. + def sort_by_length(my_sentence) my_sentence_array = my_sentence.split(" ") i = 1 + while i < my_sentence_array.length - value = i + comparison_index = i j = i - 1 + i.times do - if my_sentence_array[value].length < my_sentence_array[j].length - new_value = my_sentence_array[j] - new_j = my_sentence_array[value] + if my_sentence_array[comparison_index].length < my_sentence_array[j].length + new_comparison_index = my_sentence_array[j] + new_j = my_sentence_array[comparison_index] - my_sentence_array[value] = new_value + my_sentence_array[comparison_index] = new_comparison_index my_sentence_array[j] = new_j end j -= 1 - value -= 1 + comparison_index -= 1 end i += 1 end return my_sentence_array end - - -puts sort_by_length("words are the a") \ No newline at end of file