-
Notifications
You must be signed in to change notification settings - Fork 48
Leaves - Mariya Burrows #30
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,61 @@ | ||
| # A method to reverse the words in a sentence, in place. | ||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n ^2) because lines 47-55 contain a nested loop. | ||
| # Space complexity: ? O(n) because of the empty_index_positions array from lines 33 - 38 | ||
|
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. I would say O(m) where m is the number of words, but yet. Could you instead do this with O(1) space complexity? |
||
|
|
||
| def swap_chars(arr, index_one, index_two) | ||
| tmp = arr[index_two] | ||
| arr[index_two] = arr[index_one] | ||
| arr[index_one] = tmp | ||
| end | ||
|
|
||
| def reverse_sub_str(array, start_position, end_position) | ||
| ((end_position - start_position + 1) / 2).times do | ||
| swap_chars(array, start_position, end_position) | ||
| start_position += 1 | ||
| end_position -= 1 | ||
| end | ||
| return array | ||
| end | ||
|
|
||
| def reverse_sentence(my_sentence) | ||
| raise NotImplementedError | ||
|
|
||
| if my_sentence == "" | ||
| return "" | ||
| elsif my_sentence == nil | ||
| return nil | ||
| end | ||
|
|
||
|
|
||
| i = 0 | ||
| j = (my_sentence.length) -1 | ||
| reverse_sub_str(my_sentence, i, j) | ||
|
|
||
| empty_index_positions = [] | ||
| my_sentence.length.times do |i| | ||
| if my_sentence[i] == " " | ||
| empty_index_positions << i | ||
| end | ||
| end | ||
|
|
||
| if empty_index_positions.length == 0 | ||
| i = 0 | ||
| j = (my_sentence.length) -1 | ||
| reverse_sub_str(my_sentence, i, j) | ||
| return my_sentence | ||
| end | ||
|
|
||
| empty_index_positions.length.times do |i| | ||
| if i == 0 | ||
| reverse_sub_str(my_sentence, i, empty_index_positions[i] - 1) | ||
| else | ||
| start_position = empty_index_positions[i - 1] + 1 | ||
| end_position = empty_index_positions[i] - 1 | ||
| reverse_sub_str(my_sentence, start_position, end_position) | ||
| end | ||
| end | ||
|
|
||
| i = empty_index_positions[-1] + 1 | ||
| j = my_sentence.length - 1 | ||
| reverse_sub_str(my_sentence, i, j ) | ||
| return my_sentence | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,30 @@ | ||
| # 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(n) because of splitting the string at the beginning of the method | ||
|
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. yup |
||
|
|
||
| def swap(arr, index_one, index_two) | ||
| tmp = arr[index_two] | ||
| arr[index_two] = arr[index_one] | ||
| arr[index_one] = tmp | ||
| end | ||
|
|
||
|
|
||
| def sort_by_length(my_sentence) | ||
| raise NotImplementedError, "Method not implemented" | ||
| my_sentence = my_sentence.split | ||
|
|
||
| j = 0 # outer counter | ||
| my_sentence.length.times do | ||
| comparison_word = my_sentence[j] | ||
| for i in j...(my_sentence.length) | ||
| if my_sentence[i].length < comparison_word.length | ||
| puts my_sentence[i] | ||
| puts comparison_word | ||
| swap(my_sentence, i, j ) | ||
| comparison_word = my_sentence[j] | ||
| end | ||
|
Comment on lines
+20
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. This is an interesting sort, it's not one of the standard sorts but does sort elements. It does work however. |
||
| end | ||
| j += 1 | ||
| end | ||
| return my_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.
Actually since
start_positionjumps to the end of the reversed word after a reversal, this is O(n)