-
Notifications
You must be signed in to change notification settings - Fork 48
Leaves -- Cloudy #46
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 -- Cloudy #46
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,37 @@ | ||
| # A method to reverse the words in a sentence, in place. | ||
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def reverse_sentence(my_sentence) | ||
| raise NotImplementedError | ||
|
|
||
| # min = my_sentence[0] | ||
| # max = my_sentence[-1] | ||
| # my_arr = #["yoda"__"is"___"awesome!"].dup and add in spaces to new array[?] | ||
| # my_arr = ["yoda", "__", "is", "___", "awesome"] | ||
| # my_new_arr = [] | ||
| # loop thru | ||
| # first = my_arr[0] | ||
| # last = my_arr[-1] | ||
| # first, last = last, first | ||
| # last += 1 | ||
| # first -= -1 | ||
| # shovel into my_new_array | ||
| # in a perfect world, would return in order we want | ||
|
|
||
| def reverse(sentence) | ||
| initial = 0 | ||
| last = sentence.length - 1 | ||
|
|
||
| while initial < last | ||
| memo = sentence[initial] | ||
| sentence[initial] = sentence[last] | ||
|
|
||
| sentence[last] = memo | ||
|
|
||
| initial += 1 | ||
| last -= 1 | ||
|
|
||
| return 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. This return should be outside the |
||
| end | ||
| end | ||
|
|
||
| puts reverse("y__i____c") | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,34 @@ | |
| # sorted by the length of the word. | ||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
|
|
||
|
|
||
| # Sky_array =[ The sky is blue ].split (“ “) | ||
| # => [“the”, “sky”, “is”, “blue"] | ||
| # sky_array = 3 3 2 4#character count | ||
| # find min, then find max | ||
| #range loop, min to max, | ||
| # Output = [ is the sky blue ] | ||
|
|
||
| sky_arr = "the sky is blue" | ||
| def sort_by_length(my_sentence) | ||
| raise NotImplementedError, "Method not implemented" | ||
| return nil if my_sentence == nil | ||
| final_arr = [] | ||
| sky_arr = my_sentence.split(" ") | ||
| length_arr = sky_arr.map do |word| | ||
| word.length | ||
| end | ||
|
|
||
| min = length_arr.min | ||
| max = length_arr.max | ||
|
|
||
| (min..max).each do |length| | ||
| sky_arr.each do |word| | ||
| if word.length == length | ||
| final_arr << word | ||
| end | ||
| end | ||
|
Comment on lines
+26
to
+31
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 looks like it works, but it's a little inefficient. Instead you could just do a bubblesort using the string lengths. |
||
| end | ||
| return final_arr | ||
| end | ||
| puts sort_by_length(sky_arr) | ||
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 method reverses a string, it doesn't reverse the words in a sentence. You're on the right track.
Consider: