-
Notifications
You must be signed in to change notification settings - Fork 47
leaves- Natalie Tapias #20
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
da655c8
3d64699
ecb9c88
d9c85e4
2a08d1d
8799444
4c16074
afafdd1
06aeb4b
9280579
8697615
4c617d3
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,54 @@ | ||
| # A method to reverse the words in a sentence, in place. | ||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n^3) | ||
| # Space complexity: O(n) I believe since there is a second array created in memory to store/reverse each word | ||
|
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. Yes since you're doing You were asked to reverse the words in place. |
||
| require 'pry' | ||
|
|
||
| def reverse_sentence(my_sentence) | ||
| if my_sentence == "" | ||
| return "" | ||
| elsif my_sentence == nil | ||
| return nil | ||
| end | ||
|
|
||
| length = my_sentence.length | ||
| reversaroo(my_sentence, length) | ||
|
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) |
||
| # first fully reverse the strings | ||
|
|
||
| # take each 'word' and split into an array. then reversaroo the each word and store in an array called word_in_order_array | ||
| split_sentence = my_sentence.split(' ') | ||
|
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) |
||
| word_in_order_array = [] | ||
| split_sentence.each_with_index do |word, i| | ||
|
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(m * w) where m is the number of words and w the length of the words. |
||
| length = word.length | ||
| temp = reversaroo(word, length) | ||
| word_in_order_array.push(temp) | ||
| end | ||
|
|
||
| x = 0 | ||
| word_in_order_array.each do |word| | ||
|
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(m) |
||
| word.each_char do |char| | ||
| until my_sentence[x] != " " | ||
| x += 1 | ||
| end | ||
| my_sentence[x] = char | ||
| x += 1 | ||
| end | ||
| end | ||
| # search for any character or whitespace followed immediately by a character | ||
| return my_sentence | ||
| raise NotImplementedError | ||
| end | ||
|
|
||
|
|
||
| def reversaroo(string, length) | ||
|
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. Could this method be extended with a start and end index to reverse a substring in place? |
||
| j = (length - 1) | ||
| i = 0 | ||
| while i < j | ||
| temp = string[i] | ||
| string[i] = string[j] | ||
| string[j] = temp | ||
| j -= 1 | ||
| i += 1 | ||
| end | ||
| return string | ||
| end | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,36 @@ | ||
| # 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) constat I believe | ||
| def sort_by_length(my_sentence) | ||
| if my_sentence == "" | ||
| return [] | ||
| end | ||
|
|
||
| array = my_sentence.split(' ') | ||
| # longest_string = array.first | ||
| length = array.length | ||
|
|
||
| # If the inner loop runs with no swaps, exit | ||
| swapped = true | ||
| i = 0 | ||
| while i < length-1 && swapped # outer loop | ||
| j = 0 | ||
| # Assume you won't have to make a swap | ||
| swapped = false | ||
| 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 | ||
| # Since we just made a swap, set swapped to true | ||
| swapped = true | ||
| end | ||
| j += 1 | ||
| end | ||
| i += 1 | ||
| end | ||
|
|
||
| return array | ||
| raise NotImplementedError, "Method not implemented" | ||
| 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.
The time complexity is not O(n3).
Instead you have:
O(n + n + m * w + m)
Since you will have relatively fewer words than letters (where n is the number of letters), and most words will be short, you could say this is
O(n)time complexity.