-
Notifications
You must be signed in to change notification settings - Fork 48
Leaves - Dominique #43
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,67 @@ | ||
|
|
||
| # A method to reverse the words in a sentence, in place. | ||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) because the algorithm becuase the time complexity will change as the size of the input changes | ||
| # Space complexity: O(1) because the same number of variables will be made regardless of input size | ||
| def reverse_sentence(my_sentence) | ||
| raise NotImplementedError | ||
|
|
||
| return my_sentence if my_sentence == nil | ||
|
|
||
| counter = 0 | ||
| word_length = 0 | ||
| index = 0 | ||
|
|
||
| word_start = 0 | ||
| # needs to be an outer loop that iterates thru the sentence | ||
| while counter < my_sentence.length | ||
|
|
||
| word_length = 0 | ||
|
|
||
| while my_sentence[counter] != " " && counter < my_sentence.length | ||
| # whatever index is when the loop exits, is where the word ends | ||
| word_length += 1 | ||
| counter += 1 | ||
| end | ||
|
|
||
| word_end = counter - 1 | ||
|
|
||
| while my_sentence[counter] == " " | ||
| counter += 1 | ||
| end | ||
|
|
||
| # this is going to swap each letter in each word | ||
| word_reverse(my_sentence, word_start, word_end) | ||
|
|
||
| word_start = counter | ||
| end | ||
|
|
||
| i = 0 | ||
|
|
||
| until i == my_sentence.length/2 | ||
| temp = my_sentence[i] | ||
| my_sentence[i] = my_sentence[my_sentence.length - i - 1] | ||
| my_sentence[my_sentence.length - i - 1] = temp | ||
| i += 1 | ||
| end | ||
|
|
||
| return my_sentence | ||
|
|
||
| end | ||
|
|
||
| def word_reverse(my_sentence, word_start, word_end) | ||
| word_length = word_end - word_start + 1 | ||
|
|
||
| counter = 0 | ||
|
|
||
| while counter != word_length/2 | ||
| temp = my_sentence[word_start] | ||
| my_sentence[word_start] = my_sentence[word_end] | ||
| my_sentence[word_end] = temp | ||
|
|
||
| word_start += 1 | ||
| word_end -= 1 | ||
| counter += 1 | ||
| end | ||
| end | ||
|
|
||
|
Comment on lines
+50
to
+65
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 love this helper method! 👍 |
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,28 @@ | ||
| # 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) because as the algorithm scales, each nested loop will run n number of times according to the input. | ||
| # Space complexity: O(1) because the same amount of memory will be used regardless of the input size. | ||
|
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 are doing |
||
| def sort_by_length(my_sentence) | ||
|
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. Nice SmartBubble sort! |
||
| raise NotImplementedError, "Method not implemented" | ||
| sentence_array = my_sentence.split | ||
|
|
||
| i = 0 | ||
| swapped = true | ||
|
|
||
| while i < sentence_array.length - 1 && swapped | ||
| j = 0 | ||
| swapped = false | ||
| while j < sentence_array.length - i - 1 | ||
| if sentence_array[j].length > sentence_array[j + 1].length | ||
| temp = sentence_array[j] | ||
| sentence_array[j] = sentence_array[j + 1] | ||
| sentence_array[j + 1] = temp | ||
| swapped = true | ||
| end | ||
| j += 1 | ||
| end | ||
| i += 1 | ||
| end | ||
| return sentence_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 could also use the
word_reversemethod again to reverse the whole string.