-
Notifications
You must be signed in to change notification settings - Fork 47
Branches, Kristy #32
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?
Branches, Kristy #32
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) | ||
| # 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're doing |
||
|
|
||
| def reverse_sentence(my_sentence) | ||
| raise NotImplementedError | ||
|
|
||
| if my_sentence == "" | ||
| return "" | ||
| elsif my_sentence == nil | ||
| return nil | ||
| end | ||
| # regex credit goes to Amal | ||
| reverse = my_sentence.scan(/(\s*)(\w*'*\w+[!,.?]*)(\s*)/).flatten | ||
| i = 0 | ||
| j = reverse.length - 1 | ||
|
|
||
| (reverse.length / 2).times do | ||
| temp = reverse[i] | ||
| reverse[i] = reverse[j] | ||
| reverse[j] = temp | ||
| i += 1 | ||
| j -= 1 | ||
| end | ||
| my_sentence = reverse.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. See my lesson on how functions work This isn't changing the original argument. |
||
| return my_sentence | ||
|
|
||
|
|
||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,21 @@ | ||
| # 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) | ||
|
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 O(n2). Check out your nested loops. |
||
| # Space complexity: O(n) | ||
| def sort_by_length(my_sentence) | ||
| raise NotImplementedError, "Method not implemented" | ||
| my_array = my_sentence.split(" ") | ||
| i = 1 | ||
|
|
||
| while i < my_array.length | ||
| to_insert = my_array[i] | ||
| j = i | ||
|
|
||
| while j > 0 && my_array[j-1].length > to_insert.length | ||
| my_array[j] = my_array[j-1] | ||
| j -= 1 | ||
| end | ||
| my_array[j] = to_insert | ||
| i += 1 | ||
| end | ||
|
Comment on lines
+9
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. Nice insertion sort. |
||
| return my_array | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,74 +1,76 @@ | ||
| require_relative "test_helper" | ||
| require 'pry' | ||
|
|
||
| describe "reverse sentence" do | ||
| describe "basic tests" do | ||
| it "reverse a sentence with two words" do | ||
| test_string = "hello, world" | ||
|
|
||
| reverse_sentence(test_string) | ||
|
|
||
| test_string.must_equal "world hello," | ||
| end | ||
|
|
||
| it "reverse a sentence with three words" do | ||
| test_string = "Yoda is awesome!" | ||
|
|
||
| reverse_sentence(test_string) | ||
|
|
||
| test_string.must_equal "awesome! is Yoda" | ||
| end | ||
| end | ||
|
|
||
| # check for edge cases | ||
| describe "edge cases" do | ||
| # if it's a string parameter, check for empty | ||
| it "reverse an empty sentence" do | ||
| test_string = "" | ||
|
|
||
| reverse_sentence(test_string) | ||
|
|
||
| test_string.must_be_empty | ||
| end | ||
|
|
||
| # if the parameter is an object, check for nil | ||
| it "nil object passed to sentence reverse" do | ||
| test_string = nil | ||
|
|
||
| reverse_sentence(test_string) | ||
|
|
||
| test_string.must_be_nil | ||
| end | ||
|
|
||
| it "reverse a sentence with one word" do | ||
| test_string = "world" | ||
|
|
||
| reverse_sentence(test_string) | ||
|
|
||
| test_string.must_equal "world" | ||
| end | ||
|
|
||
| it "reverse a sentence with multiple words" do | ||
| test_string = "I'm a better engineer today than I was yesterday." | ||
|
|
||
| reverse_sentence(test_string) | ||
|
|
||
| test_string.must_equal "yesterday. was I than today engineer better a I'm" | ||
| end | ||
|
|
||
| it "reverse a sentence with multiple spaces between words" do | ||
| test_string = "How do you like them apples?" | ||
|
|
||
| reverse_sentence(test_string) | ||
|
|
||
| test_string.must_equal "apples? them like you do How" | ||
| end | ||
|
|
||
| it "reverse a sentence with preceeding and trailing white spaces" do | ||
| test_string = " I can do this! " | ||
|
|
||
| reverse_sentence(test_string) | ||
|
|
||
| test_string.must_equal " this! do can I " | ||
| end | ||
| end | ||
| end | ||
|
|
||
| reverse_sentence(test_string) | ||
|
|
||
| test_string.must_equal "apples? them like you do How" | ||
| end | ||
|
|
||
| it "reverse a sentence with preceeding and trailing white spaces" do | ||
| test_string = " I can do this! " | ||
|
|
||
| reverse_sentence(test_string) | ||
|
|
||
| test_string.must_equal " this! do can I " | ||
| end | ||
| 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.
No comprehension question answers.