-
Notifications
You must be signed in to change notification settings - Fork 48
Leaves - Elizabeth #44
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?
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,23 @@ | ||
| # A method to reverse the words in a sentence, in place. | ||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) because the amount of time it will take will increase as the length of the input increases. | ||
| # Space complexity: O(n) because the space increases as the inout increases. | ||
| def reverse_sentence(my_sentence) | ||
| raise NotImplementedError | ||
| words = my_sentence.split(" ") | ||
| length = words.length | ||
|
|
||
| mid = (length)/2 | ||
| i = 0 | ||
| k = 1 | ||
| while i < mid do | ||
| j = words[length-k] | ||
| words[length-k] = words[i] | ||
| words[i] = j | ||
| i += 1 | ||
| k += 1 | ||
| end | ||
|
|
||
| if i == mid | ||
| my_sentence = 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. Take a look at my how functions work lesson. Doing this doesn't affect the argument, which is why the tests fail. |
||
| return my_sentence | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,31 @@ | ||
| # 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) quadratice because there is a while loop with an if statemeny inside of it. | ||
| # Space complexity: O(n) linear because the array will increase the same size of the increas in size of the string. | ||
| def sort_by_length(my_sentence) | ||
| raise NotImplementedError, "Method not implemented" | ||
| if my_sentence.nil? || my_sentence == "" | ||
| return [] | ||
| end | ||
| word_array = my_sentence.split(" ") | ||
| sorted = [] | ||
| sorted << word_array[0] | ||
|
|
||
|
|
||
| word_array.each do |word| | ||
| index = 0 | ||
| while index < word_array.length | ||
| if word.length < sorted[index].length | ||
| sorted.insert(index, word) | ||
| break | ||
| elsif word.length == sorted[index].length | ||
| sorted.insert(index+1,word) | ||
| break | ||
| elsif index == sorted.length-1 | ||
| sorted.insert(index+1,word) | ||
| break | ||
| end | ||
| index += 1 | ||
|
Comment on lines
+16
to
+27
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 is a very strange nonstandard sort. It works, but does a lot of extra inserts. Take a look at the textbook curriculum lesson on sorting and compare this to the standard sorts. |
||
| end | ||
| end | ||
| return sorted.uniq | ||
| 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 were asked to try to do this in place which mean O(1) space complexity. Can you see a way?