Open
Conversation
CheezItMan
reviewed
Oct 9, 2019
CheezItMan
left a comment
There was a problem hiding this comment.
Well done. You hit the learning goals here. Take a look at my comments and let me know if you have questions.
Comment on lines
+13
to
+18
| right = my_sentence.length - 1 | ||
| until left == right || right < left | ||
| my_sentence[left], my_sentence[right] = my_sentence[right], my_sentence[left] | ||
| left += 1 | ||
| right -= 1 | ||
| end |
There was a problem hiding this comment.
Question, since you reverse things twice in this method could you dry things up a bit by making a helper method?
Comment on lines
+21
to
+29
| regex = /([^ ]+)/ | ||
| reversed_words = my_sentence.scan(regex).flatten | ||
| # positions of regex matches with enum_for code originally from "Sean Hill"'s answer at | ||
| # https://stackoverflow.com/questions/5241653/ruby-regex-match-and-get-positions-of | ||
| # gets positions of starts of each matched reversed word | ||
| reversed_word_start_indices = my_sentence.enum_for(:scan, regex).map { Regexp.last_match.begin(0) } | ||
|
|
||
| # for each start of word: | ||
| reversed_word_start_indices.each_with_index do |word_start_index, match_index| |
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n^2) loop within loop, worst case same length | ||
| # Space complexity: O(1) - only creates i and j variables each time no matter input length |
There was a problem hiding this comment.
Technically O(n) since you are doing .split
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