-
Notifications
You must be signed in to change notification settings - Fork 48
C. Gutierrez #34
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?
C. Gutierrez #34
Changes from all commits
e4c60de
ed362bc
b652300
88a5f68
eb4cf41
bd9c6ca
c266e9a
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,136 @@ | ||
| # A method to reverse the words in a sentence, in place. | ||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(2n) The array is reviewed twice, once for spaces to populate the space array, and once more piecewise to do all the swaps | ||
| # 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. You are building a space array, so this would actually be: O(m) where m is the number of words. |
||
|
|
||
| require'pry' | ||
|
|
||
| def reverse_sentence(my_sentence) | ||
| raise NotImplementedError | ||
|
|
||
| # short circuit if empty sentence | ||
| if my_sentence == nil || !my_sentence.include?(" ") | ||
| return my_sentence | ||
| end | ||
|
|
||
| # determines the last index number that will be handled | ||
| last_index = my_sentence.length - 1 | ||
|
|
||
| # reverses all the characters in the string | ||
| i = 0 | ||
| my_sentence[i] | ||
| half_swap_limit = last_index / 2 + 1 | ||
|
|
||
| until i == half_swap_limit | ||
| my_sentence[i], my_sentence[last_index - i] = my_sentence[last_index - i], my_sentence[i] | ||
|
|
||
| i += 1 | ||
|
Comment on lines
+22
to
+25
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. Notice that you reverse things a few times, this could be dryed up with a helper method. |
||
| end | ||
|
|
||
| # creates and populates an array of all space characters | ||
| space_array = [] | ||
| i = 0 | ||
| my_sentence.each_char do |character| | ||
| if character == " " | ||
| space_array << i | ||
| i += 1 | ||
| else | ||
| i += 1 | ||
| end | ||
| end | ||
|
|
||
|
|
||
|
|
||
| # determination of which methods are needed | ||
| if !space_array.include? (0) | ||
| first(space_array[0], my_sentence) | ||
| end | ||
|
|
||
| if !space_array.include? (last_index) | ||
| last(space_array[-1], last_index, my_sentence) | ||
| end | ||
|
|
||
| if space_array.length > 1 # this is run if there's more than 2 words, hence if there's more than 1 space | ||
| i = 0 | ||
| p = space_array.length - 1 | ||
|
|
||
| if i + 1 == p # short-circuit for 1-letter words | ||
| first_character = space_array[0] + 1 | ||
| second_character = space_array[1] - 1 | ||
| my_sentence[first_character], my_sentence[second_character] = my_sentence[second_character], my_sentence[first_character] | ||
| else | ||
| until i + 1 == p | ||
| middle(space_array[i], space_array[i+1], my_sentence) | ||
| i += 1 | ||
| end | ||
| end | ||
| end | ||
| return my_sentence | ||
|
|
||
| end | ||
|
|
||
|
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. These methods seem pretty repetitive. Could they be condensed into one method? |
||
| def first(first_space_index_number, my_sentence) | ||
| j = 0 | ||
|
|
||
| last_index_to_swap = first_space_index_number - 1 | ||
| half_limit = last_index_to_swap / 2 | ||
|
|
||
| if last_index_to_swap.even? | ||
| until j == half_limit | ||
| my_sentence[last_index_to_swap - j], my_sentence[j] = my_sentence[j], my_sentence[last_index_to_swap - j] | ||
| j += 1 | ||
| end | ||
| else | ||
| until j == half_limit + 1 | ||
| my_sentence[last_index_to_swap - j], my_sentence[j] = my_sentence[j], my_sentence[last_index_to_swap - j] | ||
| j += 1 | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def last(last_space_index_number, last_index, my_sentence) | ||
|
|
||
| first_index_to_swap = last_space_index_number + 1 | ||
| half_limit = (last_index - first_index_to_swap) / 2 | ||
|
|
||
| j = 0 | ||
|
|
||
| if (last_index - first_index_to_swap).even? | ||
| until j == half_limit | ||
| my_sentence[first_index_to_swap + j], my_sentence[last_index - j] = my_sentence[last_index - j], my_sentence[first_index_to_swap + j] | ||
| j += 1 | ||
| end | ||
| else | ||
| until j == half_limit + 1 | ||
| my_sentence[first_index_to_swap + j], my_sentence[last_index - j] = my_sentence[last_index - j], my_sentence[first_index_to_swap + j] | ||
| j += 1 | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def middle(first_space_index_number, last_space_index_number, my_sentence) | ||
| # short-circuit for one-letter words | ||
| if last_space_index_number - first_space_index_number == 1 | ||
| return | ||
| elsif last_space_index_number - first_space_index_number == 3 | ||
| my_sentence[first_space_index_number + 1], my_sentence[last_space_index_number - 1] = my_sentence[last_space_index_number - 1], my_sentence[first_space_index_number + 1] | ||
| return | ||
| end | ||
|
|
||
| first_index_to_swap = first_space_index_number + 1 | ||
| last_index = last_space_index_number - 1 | ||
| half_limit = (last_index - first_index_to_swap) / 2 | ||
|
|
||
| j = 0 | ||
|
|
||
| if (last_space_index_number - first_space_index_number).even? | ||
| until j == half_limit | ||
| my_sentence[first_index_to_swap + j], my_sentence[last_index - j] = my_sentence[last_index - j], my_sentence[first_index_to_swap + j] | ||
| j += 1 | ||
| end | ||
| else | ||
| until j == half_limit + 1 | ||
| my_sentence[first_index_to_swap + j], my_sentence[last_index - j] = my_sentence[last_index - j], my_sentence[first_index_to_swap + j] | ||
| j += 1 | ||
| end | ||
|
|
||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,42 @@ | ||
| # 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(n2) | ||
| # 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 since you're doing |
||
|
|
||
| def sort_by_length(my_sentence) | ||
| raise NotImplementedError, "Method not implemented" | ||
| # splits the sentence | ||
| array_of_sentence = my_sentence.split | ||
|
|
||
| #short-cirtcuit retunn statement for empty strings | ||
| if array_of_sentence.length == 0 | ||
| return [] | ||
| end | ||
|
|
||
| i = 0 #internal counter | ||
|
|
||
| e = 1 #external counter | ||
|
|
||
| # bubble sort :) | ||
| until e == array_of_sentence.length - 1 | ||
|
|
||
| until i == array_of_sentence.length - 1 | ||
|
|
||
| if array_of_sentence[i].length > array_of_sentence[i + 1].length | ||
| array_of_sentence[i], array_of_sentence[i + 1] = array_of_sentence[i + 1], array_of_sentence[i] | ||
| i += 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. You could move the |
||
| else | ||
| i += 1 | ||
| end | ||
|
|
||
| end | ||
|
|
||
| e += 1 | ||
|
|
||
| end | ||
|
|
||
| #returns transformed, digested, sorted array | ||
| return_statement = array_of_sentence | ||
|
|
||
| return return_statement | ||
|
|
||
| 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.
So really O(n)