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

# Doesn't reverse in place - still working

Choose a reason for hiding this comment

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

Gotcha


def reverse_sentence(my_sentence)
raise NotImplementedError
if my_sentence == nil
return nil
elsif my_sentence == ""
return ""
end
words = my_sentence.split(/(\s)/)
return my_sentence if words.length < 2

hi = words.length - 1
low = 0
until low >= hi
bottom = words[low]
top = words[hi]
words[low] = top
words[hi] = bottom
hi -= 1
low += 1
end
my_sentence = words.join

Choose a reason for hiding this comment

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

Note my lesson in how functions work This won't change the argument. So the tests are failing.

return my_sentence
end
24 changes: 20 additions & 4 deletions lib/sort_by_length.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
# 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 this has space complexity of O(n) since you do .split


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"
end
return [] if my_sentence == ""
array = my_sentence.split(" ")
length = array.length

i = 1
while i < length
to_insert = array[i]
j = i
while j > 0 && (array[j-1].length) > (to_insert.length)
array[j] = array[j-1]
j -= 1
end
array[j] = to_insert
i += 1
end
return array
end
19 changes: 0 additions & 19 deletions test/sort_by_length.rb

This file was deleted.

19 changes: 19 additions & 0 deletions test/sort_by_length_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# require_relative "test_helper"

Choose a reason for hiding this comment

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

Why are these commented out? Made me wonder what was up.


# describe "sort_by_length" do
# it "will return an empty array for an empty string" do
# expect(sort_by_length("")).must_equal []
# end

# 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