-
Notifications
You must be signed in to change notification settings - Fork 48
Branches - Emily Ball #26
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,28 @@ | ||
| # A method to reverse the words in a sentence, in place. | ||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n^2) | ||
| # Space complexity: O(n) | ||
|
|
||
| # Doesn't reverse in place - still working | ||
|
|
||
| def reverse_sentence(my_sentence) | ||
| raise NotImplementedError | ||
| if my_sentence == nil | ||
| return nil | ||
| elsif my_sentence == "" | ||
| return "" | ||
| end | ||
| words = my_sentence.split(/(\s)/) | ||
| return my_sentence if words.length < 2 | ||
|
|
||
| hi = words.length - 1 | ||
| low = 0 | ||
| until low >= hi | ||
| bottom = words[low] | ||
| top = words[hi] | ||
| words[low] = top | ||
| words[hi] = bottom | ||
| hi -= 1 | ||
| low += 1 | ||
| end | ||
| 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. Note my lesson in how functions work This won't change the argument. So the tests are failing. |
||
| return my_sentence | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,23 @@ | ||
| # 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. Technically this has space complexity of O(n) 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 insertion sort. |
||
| raise NotImplementedError, "Method not implemented" | ||
| end | ||
| return [] if my_sentence == "" | ||
| array = my_sentence.split(" ") | ||
| length = array.length | ||
|
|
||
| i = 1 | ||
| while i < length | ||
| to_insert = array[i] | ||
| j = i | ||
| while j > 0 && (array[j-1].length) > (to_insert.length) | ||
| array[j] = array[j-1] | ||
| j -= 1 | ||
| end | ||
| array[j] = to_insert | ||
| i += 1 | ||
| end | ||
| return array | ||
| end | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # require_relative "test_helper" | ||
|
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. Why are these commented out? Made me wonder what was up. |
||
|
|
||
| # describe "sort_by_length" do | ||
| # it "will return an empty array for an empty string" do | ||
| # expect(sort_by_length("")).must_equal [] | ||
| # end | ||
|
|
||
| # it "will return an array of words, by length" do | ||
| # expect(sort_by_length("I love Ada")).must_equal ["I", "Ada", "love"] | ||
| # end | ||
|
|
||
| # it "will return an array of words by length, words that are of equal length will appear in the order they appear" do | ||
| # expect(sort_by_length("words of equal length")).must_equal ["of", "words", "equal", "length"] | ||
| # end | ||
|
|
||
| # it "will return an array of words by length, words that are of equal length will appear in the order they appear" do | ||
| # expect(sort_by_length("I love great awesome words")).must_equal ["I", "love", "great", "words", "awesome"] | ||
| # end | ||
| # 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.
Gotcha