-
Notifications
You must be signed in to change notification settings - Fork 47
Branches - Brianna K #36
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
23d99fd
28e3bfa
6b9c49b
7d7f7e6
2aed416
ae3fd81
140edf6
4b9b791
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,50 @@ | ||
| # A method to reverse the words in a sentence, in place. | ||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def reverse_sentence(my_sentence) | ||
| raise NotImplementedError | ||
| # Time complexity: O(n + m) - | ||
| # Reverse_Sentence | ||
| # - 1st while loop: O(.5n) where n is the number of letters | ||
| # - 2nd while loop: O(n) where n is the number of letters | ||
| # Reverse | ||
| # - O(m): where m is the number of words | ||
| # Space complexity: O(1) | ||
|
|
||
| def reverse(text, text_start, text_end) | ||
| i = 0 | ||
| ((text_end - text_start)/2).times do | ||
| temp_char = text[text_start + i] | ||
| text[text_start + i] = text[text_end - i - 1] | ||
| text[text_end - i - 1] = temp_char | ||
| i += 1 | ||
| end | ||
| end | ||
|
Comment on lines
+10
to
18
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. Great helper method. Could you have used it in other places in the main method? |
||
|
|
||
|
|
||
| def reverse_sentence(my_sentence) | ||
| return nil if my_sentence.nil? | ||
| letter = 0 | ||
| length = my_sentence.length | ||
|
|
||
| end_char = length - 1 | ||
| while letter < my_sentence.length/2 do | ||
| temp_char = my_sentence[letter] | ||
| my_sentence[letter] = my_sentence[length - 1 - letter] | ||
| my_sentence[length - 1 - letter] = temp_char | ||
| letter += 1 | ||
| end | ||
|
Comment on lines
+27
to
+32
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. Hmm... another reversal section |
||
|
|
||
| letter = 0 | ||
| while letter < my_sentence.length do | ||
| while my_sentence[letter] == " " | ||
| letter += 1 | ||
| end | ||
| start_index = letter | ||
|
|
||
| while my_sentence[letter] != " " && letter < my_sentence.length | ||
| letter += 1 | ||
| end | ||
| end_index = letter | ||
|
|
||
| reverse(my_sentence, start_index, end_index) | ||
| end | ||
|
|
||
| return my_sentence | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,22 @@ | ||
| # 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) | ||
| def sort_by_length(my_sentence) | ||
| raise NotImplementedError, "Method not implemented" | ||
| end | ||
| return nil if my_sentence.nil? | ||
|
|
||
| my_sorted_array = [] | ||
| my_array = my_sentence.split | ||
|
|
||
| until my_array.length == 0 do | ||
| current_word = my_array[0] | ||
| my_array.each do |word| | ||
| if word.length < current_word.length | ||
| current_word = word | ||
| end | ||
| end | ||
| my_sorted_array.push(current_word) | ||
| my_array.delete(current_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. Just note |
||
| end | ||
| return my_sorted_array | ||
| 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.
You're reversing the whole string = n
Then you go through and reverse each word for a total of < n letters reversed
So O(2n) = O(n)