Open
Conversation
CheezItMan
reviewed
Oct 9, 2019
CheezItMan
left a comment
There was a problem hiding this comment.
Nice work, check out my comments and let me know if you have any questions.
| # 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). |
There was a problem hiding this comment.
This is actually O(n) since word_start in the outer loop jumps to the end of a word after it reverses that word.
Comment on lines
+14
to
+23
| 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 |
There was a problem hiding this comment.
Notice that you're reversing things twice in this method. Could you dry things up by making a helper method?
| # 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.
The time complexity is O(n2). Look at the nested loops
Otherwise nice selection sort.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Sorting & Reverse Sentence