forked from AdaGold/string-manipulation-practice
-
Notifications
You must be signed in to change notification settings - Fork 48
Bri - Leaves #39
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
brilatimer
wants to merge
6
commits into
Ada-C12:master
Choose a base branch
from
brilatimer: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
Bri - Leaves #39
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1f0735d
wave 1 complete
brilatimer 7420c59
solved not in place whoops
brilatimer aeb98d8
method to reverse based on index
brilatimer e48f7dd
completed wave 2
brilatimer c67bfbc
time and space complexity answered
brilatimer db64e6a
removed binding.pry
brilatimer 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,48 @@ | ||
| # A method to reverse the words in a sentence, in place. | ||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def reverse_sentence(my_sentence) | ||
| raise NotImplementedError | ||
| # Time complexity: O(N) | ||
| # Space complexity: O(N) | ||
|
|
||
| # reverse string given start and ending index | ||
| def reverse_word(string, start, ending) | ||
|
|
||
| i = start | ||
| j = ending | ||
|
|
||
| while i < j | ||
| temp = string[i] | ||
| string[i] = string[j] | ||
| string[j] = temp | ||
|
|
||
| i += 1 | ||
| j -= 1 | ||
| end | ||
| end | ||
|
Comment on lines
+6
to
+19
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. Love the helper method! |
||
|
|
||
| # call method made to reverse sentence | ||
|
|
||
| def reverse_sentence(string) | ||
| if string == nil | ||
| return | ||
| end | ||
| reverse_word(string, 0, string.length - 1) | ||
| start = 0 | ||
| while start < string.length | ||
| if string[start] == " " | ||
| start += 1 | ||
| next | ||
|
|
||
| else # found the start of a word | ||
| ending = start | ||
| while ending <= string.length | ||
| if string[ending] == " " || ending == string.length | ||
| reverse_word(string, start, ending - 1) | ||
| start = ending | ||
| break | ||
| end | ||
|
|
||
| ending += 1 | ||
|
|
||
| end | ||
| 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) | ||
| # Space complexity: O(N) | ||
|
|
||
| def sort_by_length(my_sentence) | ||
|
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. Nice Bubblesort! |
||
| raise NotImplementedError, "Method not implemented" | ||
| sentence_array = my_sentence.split(" ") | ||
| bubble_sort(sentence_array, sentence_array.length) | ||
| return sentence_array | ||
| end | ||
|
|
||
| # cite: https://github.com/Ada-Developers-Academy/textbook-curriculum/blob/master/04-cs-fundamentals/classroom/Sorting.md | ||
| def bubble_sort(array, length) | ||
| i = 0 | ||
| while i < length - 1 # outer loop | ||
| j = 0 | ||
| while j < length-i-1 # inner loop | ||
| if array[j].length > array[j+1].length # swap if length of word is greater than other | ||
| temp = array[j] | ||
| array[j] = array[j+1] | ||
| array[j+1] = temp | ||
| end | ||
| j += 1 | ||
| end | ||
| i += 1 | ||
| end | ||
| end | ||
File renamed without changes.
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.
Since you're not making another string or array, this is actually O(1) space complexity.