-
Notifications
You must be signed in to change notification settings - Fork 48
Raisah V #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Raisah V #29
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
|
Comment on lines
+14
to
+23
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Notice that you're reversing things twice in this method. Could you dry things up by making a helper method? |
||
|
|
||
|
|
||
| # 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 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The time complexity is O(n2). Look at the nested loops Otherwise nice selection sort. |
||
|
|
||
| # 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 | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is actually O(n) since
word_startin the outer loop jumps to the end of a word after it reverses that word.