diff --git a/lib/reverse_sentence.rb b/lib/reverse_sentence.rb index 3fe7cac..6d07aed 100644 --- a/lib/reverse_sentence.rb +++ b/lib/reverse_sentence.rb @@ -1,6 +1,65 @@ # A method to reverse the words in a sentence, in place. -# Time complexity: ? -# Space complexity: ? +# 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) - raise NotImplementedError + + 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_i = my_sentence[i] + temp_j = my_sentence[j] + + my_sentence[i] = temp_j + my_sentence[j] = temp_i + + i += 1 + j -= 1 + end + + + # reverses any words in the sentence + i = 0 + word_start = 0 + word_end = 0 + in_word = true + + while i < my_sentence.length + if ( my_sentence[i] == " " || i == my_sentence.length - 1 ) && in_word == true + + word_end = i + word_end -= 1 if my_sentence[i] == " " + + word_length = word_end - word_start + 1 + count = 0 + + while count < word_length / 2 + temp_start = my_sentence[word_start] + temp_end = my_sentence[word_end] + + my_sentence[word_start] = temp_end + my_sentence[word_end] = temp_start + + word_start += 1 + word_end -= 1 + count += 1 + end + + in_word = false + end + + if my_sentence[i] != " " && in_word == false + word_start = i + in_word = true + end + + i += 1 + end + + return my_sentence end diff --git a/lib/sort_by_length.rb b/lib/sort_by_length.rb index a5713ad..2c14596 100644 --- a/lib/sort_by_length.rb +++ b/lib/sort_by_length.rb @@ -1,7 +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) - raise NotImplementedError, "Method not implemented" + my_sentence_array = my_sentence.split(" ") + + i = 1 + + while i < my_sentence_array.length + comparison_index = i + j = i - 1 + + i.times do + 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[comparison_index] = new_comparison_index + my_sentence_array[j] = new_j + end + j -= 1 + comparison_index -= 1 + end + i += 1 + end + + return my_sentence_array end