forked from AdaGold/string-manipulation-practice
-
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
Open
geli-gel
wants to merge
5
commits into
Ada-C12:master
Choose a base branch
from
geli-gel:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Angele Z. #27
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4c27370
renamed sort_by_length.rb to sort_by_length_test.rb
geli-gel a44a607
sort by length working, need to add complexity answers and remove exc…
geli-gel c7fa62a
tests passing
geli-gel bd3fce2
removed unnecessary if block
geli-gel b5e3d2f
added source for match indices code
geli-gel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,40 @@ | ||
| # A method to reverse the words in a sentence, in place. | ||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n + n/2 + n + n/2) = O(n) | ||
| # it basically goes n times with n being how long the | ||
| # sentence is (in letters) | ||
| # Space complexity: O(left and right and regex and reversed words) = O(n) | ||
| # it will need to create a left and right and regex variable each time that | ||
| # won't change depending on input, but the list of words will increase by n | ||
| # if n increases (in number of words) | ||
| def reverse_sentence(my_sentence) | ||
| raise NotImplementedError | ||
| return if my_sentence == nil || my_sentence.length <= 1 | ||
| # loop through my_sentence, reversing it from last to first | ||
| left = 0 | ||
| 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 | ||
|
|
||
| # find all starts of reversed words and the words | ||
| 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| | ||
|
Comment on lines
+21
to
+29
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. Clever |
||
| # now that the sentence is reversed the words need to be reversed | ||
| # which will put them back in order. | ||
| left = word_start_index | ||
| right = left + reversed_words[match_index].length - 1 | ||
| until left == right || right < left | ||
| my_sentence[left], my_sentence[right] = my_sentence[right], my_sentence[left] | ||
| left += 1 | ||
| right -= 1 | ||
| end | ||
| end | ||
| end | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,27 @@ | ||
| # 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: 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 commentThe reason will be displayed to describe this comment to others. Learn more. Technically O(n) since you are doing |
||
| def sort_by_length(my_sentence) | ||
| raise NotImplementedError, "Method not implemented" | ||
| # split my_sentence by words assuming spaces separate them | ||
| # insertion_sort them in here, returning array of words | ||
| words = my_sentence.split(" ") | ||
| # loop through from 2nd item to end, outer loop increasing by one, | ||
| i = 1 | ||
| while i < words.length | ||
| # set variable equal to "to_be_inserted" | ||
| to_be_inserted = words[i] | ||
| j = i | ||
| # from j to the beginning, and until j is less than value at [j-1] | ||
| while j > 0 && words[j-1].length > to_be_inserted.length | ||
| # check the value at [j-1], if value at [j-1] is greater than value at [j] then swap their values | ||
| if words[j-1].length > words[j].length | ||
| words[j], words[j-1] = words[j-1], words[j] | ||
| end | ||
| j -= 1 | ||
| end | ||
| words[j] = to_be_inserted | ||
| i += 1 | ||
| end | ||
| return words | ||
| end | ||
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
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.
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?