-
Notifications
You must be signed in to change notification settings - Fork 48
Leaves - Yitgop #35
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 - Yitgop #35
Changes from 5 commits
a093f2c
5db16ea
0910b51
cefb578
1eea754
c29ac41
37d9a19
297c806
6aebf82
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 |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| GEM | ||
| remote: http://rubygems.org/ | ||
| specs: | ||
| ansi (1.5.0) | ||
| builder (3.2.3) | ||
| minitest (5.10.3) | ||
| minitest-reporters (1.3.8) | ||
| ansi | ||
| builder | ||
| minitest (>= 5.0) | ||
| ruby-progressbar | ||
| minitest-skip (0.0.1) | ||
| minitest (~> 5.0) | ||
| rake (12.3.0) | ||
| ruby-progressbar (1.10.1) | ||
|
|
||
| PLATFORMS | ||
| ruby | ||
|
|
||
| DEPENDENCIES | ||
| minitest | ||
| minitest-reporters | ||
| minitest-skip | ||
| rake | ||
|
|
||
| RUBY VERSION | ||
| ruby 2.5.1p57 | ||
|
|
||
| BUNDLED WITH | ||
| 1.17.3 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,73 @@ | ||
| # 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(n2) | ||
| # Space complexity: O(n) | ||
| def reverse_sentence(my_sentence) | ||
| #Step_0: Screen out all sentences that doesn't need to be/can't be reversed | ||
| if my_sentence == nil || my_sentence == "" || my_sentence.include?("\s") == false | ||
| return my_sentence | ||
| exit | ||
| end | ||
|
|
||
| #Step_1: Reverse all characters within the string in place | ||
| sentence_pairs = pairs_helper(my_sentence.length) | ||
| i = 1 | ||
| (sentence_pairs).times do | ||
| temp = my_sentence[i-1] | ||
| my_sentence[i-1] = my_sentence[-i] | ||
| my_sentence[-i] = temp | ||
| i += 1 | ||
|
||
| end | ||
|
|
||
| #Step_2: Find all whitespace index(es) within the current string | ||
| space_indexes = (0...my_sentence.length).find_all { |c| my_sentence[c] == " " } | ||
|
|
||
| #Step_3: Reverse all characters for each word based on the whitespace index(es) | ||
| #Step_3.1: Reverse the first word in the string | ||
| word_pairs = pairs_helper(space_indexes[0]) | ||
| i = 1 | ||
| (word_pairs).times do | ||
| temp = my_sentence[i-1] | ||
| my_sentence[i-1] = my_sentence[space_indexes[0]-i] | ||
| my_sentence[space_indexes[0]-i] = temp | ||
| i += 1 | ||
| end | ||
| #Step_3.2: reverse the last word in the string | ||
| word_pairs = pairs_helper((my_sentence.length - 1) - space_indexes[-1]) | ||
| i = 1 | ||
| (word_pairs).times do | ||
| temp = my_sentence[-i] | ||
| my_sentence[-i] = my_sentence[space_indexes[-1]+i] | ||
| my_sentence[space_indexes[-1]+i] = temp | ||
| i += 1 | ||
| end | ||
| #Step_3.3: reverse the words in between (if any) | ||
| i = 0 | ||
| if space_indexes.length == 1 | ||
| return my_sentence | ||
| else | ||
| until i == (space_indexes.length - 1) | ||
| if my_sentence[space_indexes[i]+1] != " " | ||
| word_pairs = pairs_helper(space_indexes[i+1]-space_indexes[i]-1) | ||
| n = 1 | ||
| (word_pairs).times do | ||
| temp = my_sentence[space_indexes[i]+n] | ||
| my_sentence[space_indexes[i]+n] = my_sentence[space_indexes[i+1]-n] | ||
| my_sentence[space_indexes[i+1]-n] = temp | ||
| n += 1 | ||
| end | ||
| end | ||
| i += 1 | ||
| end | ||
| return my_sentence | ||
| end | ||
| end | ||
|
|
||
| # Helper method to determin pairs of character that needs to be swapped | ||
| def pairs_helper(length) | ||
| if length % 2 == 0 | ||
| pairs = length / 2 | ||
| else | ||
| pairs = (length - 1) / 2 | ||
| end | ||
| return pairs | ||
|
||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,24 @@ | ||
| # A method which will return an array of the words in the string | ||
| # sorted by the length of the word. | ||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # sorted by the length of the word. | ||
| # Time complexity: O(n2) | ||
| # Space complexity: O(n) | ||
|
|
||
| def sort_by_length(my_sentence) | ||
|
||
| raise NotImplementedError, "Method not implemented" | ||
| end | ||
| #Step_1: break the sentence into an array of words | ||
| my_array = my_sentence.split(/\W+/) | ||
| #Step_2: compare the words and sort the array | ||
| i = 0 | ||
| while i < my_array.length-1 | ||
| j = 0 | ||
| while j < my_array.length-i-1 | ||
| if my_array[j].length > my_array[j+1].length | ||
| temp = my_array[j] | ||
| my_array[j] = my_array[j+1] | ||
| my_array[j+1] = temp | ||
| end | ||
| j += 1 | ||
| end | ||
| i += 1 | ||
| end | ||
| return my_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.
Correct where n is the number of words.