-
Notifications
You must be signed in to change notification settings - Fork 48
Leaves - Katie #45
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?
Leaves - Katie #45
Changes from all commits
bd833f3
e3fe1a4
1b9ef19
2418f32
3cb0536
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,52 @@ | ||
| # A method to reverse the words in a sentence, in place. | ||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # A method to reverse the words in a sentence, in place. | ||
| # Time complexity: O(n^3) | ||
| # Space complexity: O(1) | ||
| def reverse_sentence(my_sentence) | ||
| raise NotImplementedError | ||
| if my_sentence == nil | ||
| return nil | ||
| end | ||
| my_sentence = string_reverse(my_sentence) | ||
| my_sentence = word_reverse(my_sentence) | ||
| return my_sentence | ||
| end | ||
|
|
||
| def string_reverse(string) | ||
|
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. 👍 O(n) |
||
| i = 0 | ||
| j = string.length - 1 | ||
| temp_i = "" | ||
| temp_j = "" | ||
| while i < j do | ||
| temp_i = string[i] | ||
| temp_j = string[j] | ||
| string[i] = temp_j | ||
| string[j] = temp_i | ||
| i+=1 | ||
| j-=1 | ||
| end | ||
| return string | ||
| end | ||
|
|
||
| def word_reverse(string) | ||
|
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. 👍 O(n) |
||
| i = 0 | ||
| j = 0 | ||
| length = string.length | ||
| while i < length | ||
| while string[j] != " " && j < length do | ||
| j += 1 | ||
| end | ||
| j -= 1 | ||
| word_start = i | ||
| word_end = j | ||
| while word_start < word_end do | ||
| temp_i = string[word_start] | ||
| temp_j = string[word_end] | ||
| string[word_start] = temp_j | ||
| string[word_end] = temp_i | ||
| word_start+=1 | ||
| word_end-=1 | ||
| end | ||
|
Comment on lines
+40
to
+47
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 does the same logic as reverse_sentence, so why not make the method flexible enough to be used in both places. |
||
| i = j+2 | ||
| j = i | ||
| end | ||
| return string | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,26 @@ | ||
| # 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: On | ||
|
|
||
| def sort_by_length(my_sentence) | ||
| raise NotImplementedError, "Method not implemented" | ||
| sorted_sentence = my_sentence.split(" ") | ||
| bubble_sort(sorted_sentence, sorted_sentence.length) | ||
| return sorted_sentence | ||
| end | ||
|
|
||
| def bubble_sort(array, length) | ||
| i = 0 | ||
| while i < length-1 # outer loop | ||
| j = 0 | ||
| while j < length-i-1 # inner loop | ||
| if array[j].length > array[j+1].length # swap | ||
| temp = array[j] | ||
| array[j] = array[j+1] | ||
| array[j+1] = temp | ||
| end | ||
| j += 1 | ||
| end | ||
| i += 1 | ||
| 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.
This is actually O(n) as a solution.