-
Notifications
You must be signed in to change notification settings - Fork 48
Yasmin-Leaves #37
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?
Yasmin-Leaves #37
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 |
|---|---|---|
| @@ -1,6 +1,31 @@ | ||
| #require "pry" | ||
| # A method to reverse the words in a sentence, in place. | ||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: o(n) | ||
| # Space complexity: o(1) | ||
| def reverse_sentence(my_sentence) | ||
| raise NotImplementedError | ||
| #raise NotImplementedError | ||
| if my_sentence == nil | ||
| return my_sentence | ||
| end | ||
| words = my_sentence.split() | ||
| i = 0 | ||
| j = words.length - 1 | ||
|
|
||
| while i < j do | ||
| temp = words[i] | ||
| # binding.pry | ||
| words[i] = words[j] | ||
| words[j] = temp | ||
| # binding.pry | ||
| i += 1 | ||
| j -= 1 | ||
| end | ||
| # binding.pry | ||
| # print words | ||
| final_word = words.join(" ") | ||
|
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. This works, but you assume each word is separated by one space. That's not necessarily true. |
||
| return final_word | ||
| end | ||
|
|
||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,19 @@ | ||
| # 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(1) | ||
|
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. Since you do |
||
| 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" | ||
| #raise NotImplementedError, "Method not implemented" | ||
| new_sentence = my_sentence.split() | ||
| n = new_sentence.length | ||
| for j in 0..n-1 | ||
| for i in 0..n-2-j | ||
| if new_sentence[i].length > new_sentence[i + 1].length | ||
| temp = new_sentence[i] | ||
| new_sentence[i] = new_sentence[i+1] | ||
| new_sentence[i + 1] = temp | ||
| end | ||
| end | ||
| end | ||
| return new_sentence | ||
| 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.
You are doing
my_sentence.splitwhich creates a new array of equal size to the string. So... This is O(n) space complexity.You were asked to do this in place.