-
Notifications
You must be signed in to change notification settings - Fork 48
Sara-Branches #33
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?
Sara-Branches #33
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| { | ||
| // Use IntelliSense to learn about possible attributes. | ||
| // Hover to view descriptions of existing attributes. | ||
| // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
| "version": "0.2.0", | ||
| "configurations": [ | ||
| { | ||
| "name": "Debug Local File", | ||
| "type": "Ruby", | ||
| "request": "launch", | ||
| "program": "${workspaceRoot}/main.rb" | ||
| } | ||
| ] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,35 @@ | ||
| # A method to reverse the words in a sentence, in place. | ||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: o(n^2) | ||
| # Space complexity: o(1) | ||
| def reverse_sentence(my_sentence) | ||
| raise NotImplementedError | ||
| if my_sentence == nil | ||
| return "" | ||
| end | ||
|
|
||
| i = 0 | ||
| j = 0 | ||
| while j < my_sentence.length | ||
| char = my_sentence[j] | ||
| if char == " " | ||
| reverse(my_sentence, i, j - 1) | ||
| i = j + 1 | ||
| end | ||
| j += 1 | ||
| end | ||
|
|
||
| reverse(my_sentence, i, my_sentence.length - 1) | ||
| reverse(my_sentence, 0, my_sentence.length - 1) | ||
|
|
||
| end | ||
|
|
||
| def reverse(sentence, i, j) | ||
|
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. I love the helper method |
||
| while i < j | ||
| temp = sentence[i] | ||
| sentence[i] = sentence[j] | ||
| sentence[j] = temp | ||
|
|
||
| i += 1 | ||
| j -= 1 | ||
|
|
||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,28 @@ | ||
| # 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) | ||
|
Comment on lines
+3
to
+4
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 |
||
|
|
||
| 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" | ||
| words = my_sentence.split(" ") | ||
| # print words | ||
| #************ | ||
| length = words.length | ||
| i = 0 | ||
| while i < length - 1 | ||
| j = 0 | ||
| while j < length - i - 1 | ||
| if words[j].length > words[j + 1].length | ||
| temp = words[j] | ||
| words[j] = words[j + 1] | ||
| words[j + 1] = temp | ||
| end | ||
| j += 1 | ||
| end | ||
| i += 1 | ||
| end | ||
| return words | ||
| end | ||
|
|
||
|
|
||
| # print sort_by_length("abc ab a") | ||
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.
This is actually O(n), since you don't reverse the string n times, but only smaller words. If you want to be technical it's O(n * m) where m is the length of a word, but since most words are pretty small you can drop that term.