-
Notifications
You must be signed in to change notification settings - Fork 48
Angele Z. #27
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?
Angele Z. #27
Conversation
CheezItMan
left a comment
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.
Well done. You hit the learning goals here. Take a look at my comments and let me know if you have questions.
| 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question, since you reverse things twice in this method could you dry things up a bit by making a helper method?
| 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| |
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.
Clever
| # 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically O(n) since you are doing .split
Sorting & Reverse Sentence