forked from AdaGold/string-manipulation-practice
-
Notifications
You must be signed in to change notification settings - Fork 48
Branches -- Paige D. #21
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
minipaige02
wants to merge
2
commits into
Ada-C12:master
Choose a base branch
from
minipaige02: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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,72 @@ | ||
| # A method to reverse the words in a sentence, in place. | ||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) - my method does not have any nested loops, but it will run through the length of the original string multiple times | ||
| # Space complexity: O(n) - a second variable (reverse) needs to be created, which will take up as much space as the original string | ||
| def reverse_sentence(my_sentence) | ||
| raise NotImplementedError | ||
| if my_sentence != nil | ||
| # Creates array of words and spaces | ||
| words_array = words(my_sentence) | ||
|
|
||
| # Revereses the order of the words | ||
| length = words_array.length | ||
| i = 0 | ||
| j = length - 1 | ||
| (length / 2).times do | ||
| # selects word at smallest index that has not been swapped | ||
| word_1 = words_array[i] | ||
| # selects words at largest index that has not been swapped | ||
| word_2 = words_array[j] | ||
|
|
||
| words_array[i] = word_2 | ||
| words_array[j] = word_1 | ||
|
|
||
| i += 1 | ||
| j -= 1 | ||
| end | ||
|
|
||
| reverse = words_array.join | ||
|
|
||
| # Overrides original string with order of characters in reverse | ||
| i = 0 | ||
| my_sentence.length.times do | ||
| my_sentence[i] = reverse[i] | ||
| i += 1 | ||
| end | ||
| end | ||
|
|
||
| return my_sentence | ||
| end | ||
|
|
||
| # Helper method to convert sentence into array of words - O(n) time complexity | ||
| def words(my_sentence) | ||
| length = my_sentence.length | ||
| words_array = [] | ||
| i = 0 | ||
| word = "" | ||
|
|
||
| until i == length | ||
| l = my_sentence[i] | ||
| j = my_sentence[i + 1] | ||
|
|
||
| if l != " " && j != " " | ||
| word += l | ||
| elsif l != " " && j == " " | ||
| word += l | ||
| words_array << word | ||
| word = "" | ||
| elsif l == " " && j == " " | ||
| word += l | ||
| elsif l == " " && j != " " | ||
| word += l | ||
| words_array << word | ||
| word = "" | ||
| end | ||
|
|
||
| if i == length - 1 | ||
| words_array << word | ||
| end | ||
|
|
||
| i += 1 | ||
| end | ||
|
|
||
| return words_array | ||
| 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,21 @@ | ||
| # 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) | ||
|
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. Correct for a basic bubble sort. |
||
| # Space complexity: O(n) | ||
| def sort_by_length(my_sentence) | ||
| raise NotImplementedError, "Method not implemented" | ||
| new_sentence = my_sentence.split(" ") | ||
| length = new_sentence.length | ||
| i = 0 | ||
|
|
||
| (length - 1).times do | ||
| j = new_sentence[i] | ||
| if new_sentence[i].length > new_sentence[i + 1].length | ||
| new_sentence[i] = new_sentence[i + 1] | ||
| new_sentence[i + 1] = j | ||
| end | ||
|
|
||
| i += 1 | ||
| end | ||
|
|
||
| return new_sentence | ||
| 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.
You were asked to reverse the sentence in place. So think if you can do this with O(1) space complexity.