Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions lib/reverse_sentence.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,40 @@
# A method to reverse the words in a sentence, in place.
# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n + n/2 + n + n/2) = O(n)
# it basically goes n times with n being how long the
# sentence is (in letters)
# Space complexity: O(left and right and regex and reversed words) = O(n)
# it will need to create a left and right and regex variable each time that
# won't change depending on input, but the list of words will increase by n
# if n increases (in number of words)
def reverse_sentence(my_sentence)
raise NotImplementedError
return if my_sentence == nil || my_sentence.length <= 1
# loop through my_sentence, reversing it from last to first
left = 0
right = my_sentence.length - 1
until left == right || right < left
my_sentence[left], my_sentence[right] = my_sentence[right], my_sentence[left]
left += 1
right -= 1
end
Comment on lines +13 to +18

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question, since you reverse things twice in this method could you dry things up a bit by making a helper method?


# find all starts of reversed words and the words
regex = /([^ ]+)/
reversed_words = my_sentence.scan(regex).flatten
# positions of regex matches with enum_for code originally from "Sean Hill"'s answer at
# https://stackoverflow.com/questions/5241653/ruby-regex-match-and-get-positions-of
# gets positions of starts of each matched reversed word
reversed_word_start_indices = my_sentence.enum_for(:scan, regex).map { Regexp.last_match.begin(0) }

# for each start of word:
reversed_word_start_indices.each_with_index do |word_start_index, match_index|
Comment on lines +21 to +29

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clever

# now that the sentence is reversed the words need to be reversed
# which will put them back in order.
left = word_start_index
right = left + reversed_words[match_index].length - 1
until left == right || right < left
my_sentence[left], my_sentence[right] = my_sentence[right], my_sentence[left]
left += 1
right -= 1
end
end
end
26 changes: 23 additions & 3 deletions lib/sort_by_length.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
# 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) loop within loop, worst case same length
# Space complexity: O(1) - only creates i and j variables each time no matter input length

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically O(n) since you are doing .split

def sort_by_length(my_sentence)
raise NotImplementedError, "Method not implemented"
# split my_sentence by words assuming spaces separate them
# insertion_sort them in here, returning array of words
words = my_sentence.split(" ")
# loop through from 2nd item to end, outer loop increasing by one,
i = 1
while i < words.length
# set variable equal to "to_be_inserted"
to_be_inserted = words[i]
j = i
# from j to the beginning, and until j is less than value at [j-1]
while j > 0 && words[j-1].length > to_be_inserted.length
# check the value at [j-1], if value at [j-1] is greater than value at [j] then swap their values
if words[j-1].length > words[j].length
words[j], words[j-1] = words[j-1], words[j]
end
j -= 1
end
words[j] = to_be_inserted
i += 1
end
return words
end
2 changes: 1 addition & 1 deletion test/sort_by_length.rb → test/sort_by_length_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
it "will return an array of words by length, words that are of equal length will appear in the order they appear" do
expect(sort_by_length("I love great awesome words")).must_equal ["I", "love", "great", "words", "awesome"]
end
end
end