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
30 changes: 27 additions & 3 deletions lib/reverse_sentence.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
# A method to reverse the words in a sentence, in place.
# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n^2)
# Space complexity: O(1)
def reverse_sentence(my_sentence)
raise NotImplementedError
return nil if my_sentence == nil

len = my_sentence.length

(len / 2).times do |i|
temp = my_sentence[len - i - 1]
my_sentence[len - i - 1] = my_sentence[i]
my_sentence[i] = temp
Comment on lines +10 to +12

Choose a reason for hiding this comment

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

Notice that you're reversing substrings multiple times. That makes it a great candidate for a helper method.

end

first_index = 0

len.times do |j|
if my_sentence[j + 1] == " " || j == len - 1
((j - first_index + 1) / 2).times do |k|
temp = my_sentence[j - k]
my_sentence[j - k] = my_sentence[first_index + k]
my_sentence[first_index + k] = temp
end

first_index = j + 2
end
end

return my_sentence
end
30 changes: 27 additions & 3 deletions lib/sort_by_length.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,31 @@
# 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(1)

Choose a reason for hiding this comment

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

Technically since you're doing .split this makes a new array and the space complexity is O(n)

def sort_by_length(my_sentence)

Choose a reason for hiding this comment

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

Nice insertion sort!

raise NotImplementedError, "Method not implemented"

# I'm assuming split is O(n),
# can't imagine it needing more than one pass through n
sent_array = my_sentence.split(" ")
count = sent_array.length

if count == 0
return sent_array
end

i = 1
while i < count
to_insert = sent_array[i]
j = i

while j > 0 && sent_array[j-1].length > to_insert.length
sent_array[j] = sent_array[j-1]
j -= 1
end

sent_array[j] = to_insert
i += 1
end

return sent_array
end
6 changes: 3 additions & 3 deletions test/sort_by_length.rb → test/sort_by_length_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
it "will return an array of words, by length" do
expect(sort_by_length("I love Ada")).must_equal ["I", "Ada", "love"]
end

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("words of equal length")).must_equal ["of", "words", "equal", "length"]
end

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