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

Choose a reason for hiding this comment

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

You are doing my_sentence.split which creates a new array of equal size to the string. So... This is O(n) space complexity.

You were asked to do this in place.

def reverse_sentence(my_sentence)
raise NotImplementedError
#raise NotImplementedError
if my_sentence == nil
return my_sentence
end
words = my_sentence.split()
i = 0
j = words.length - 1

while i < j do
temp = words[i]
# binding.pry
words[i] = words[j]
words[j] = temp
# binding.pry
i += 1
j -= 1
end
# binding.pry
# print words
final_word = words.join(" ")

Choose a reason for hiding this comment

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

This works, but you assume each word is separated by one space. That's not necessarily true.

return final_word
end




18 changes: 15 additions & 3 deletions lib/sort_by_length.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
# 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.

Since you do .split this has space complexity of 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 bubblesort.

raise NotImplementedError, "Method not implemented"
#raise NotImplementedError, "Method not implemented"
new_sentence = my_sentence.split()
n = new_sentence.length
for j in 0..n-1
for i in 0..n-2-j
if new_sentence[i].length > new_sentence[i + 1].length
temp = new_sentence[i]
new_sentence[i] = new_sentence[i+1]
new_sentence[i + 1] = temp
end
end
end
return new_sentence
end
44 changes: 26 additions & 18 deletions test/reverse_sentence_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,70 +5,78 @@
it "reverse a sentence with two words" do
test_string = "hello, world"

reverse_sentence(test_string)
# reverse_sentence(test_string)
reverse_sentence(test_string).must_equal "world hello,"

test_string.must_equal "world hello,"
# test_string.must_equal "world hello,"
end

it "reverse a sentence with three words" do
test_string = "Yoda is awesome!"

reverse_sentence(test_string)
#reverse_sentence(test_string)

test_string.must_equal "awesome! is Yoda"
#test_string.must_equal "awesome! is Yoda"
reverse_sentence(test_string).must_equal "awesome! is Yoda"
end
end

# check for edge cases
#check for edge cases
describe "edge cases" do
# if it's a string parameter, check for empty
it "reverse an empty sentence" do
test_string = ""

reverse_sentence(test_string)

test_string.must_be_empty
#reverse_sentence(test_string)
#test_string.must_be_empty
reverse_sentence(test_string).must_be_empty
end

# if the parameter is an object, check for nil
it "nil object passed to sentence reverse" do
test_string = nil

reverse_sentence(test_string)
#reverse_sentence(test_string)

test_string.must_be_nil
#test_string.must_be_nil
reverse_sentence(test_string).must_be_nil

end

it "reverse a sentence with one word" do
test_string = "world"

reverse_sentence(test_string)
#reverse_sentence(test_string)

test_string.must_equal "world"
#test_string.must_equal "world"
reverse_sentence(test_string).must_equal "world"
end

it "reverse a sentence with multiple words" do
test_string = "I'm a better engineer today than I was yesterday."

reverse_sentence(test_string)
#reverse_sentence(test_string)

test_string.must_equal "yesterday. was I than today engineer better a I'm"
#test_string.must_equal "yesterday. was I than today engineer better a I'm"
reverse_sentence(test_string).must_equal "yesterday. was I than today engineer better a I'm"
end

it "reverse a sentence with multiple spaces between words" do
test_string = "How do you like them apples?"

reverse_sentence(test_string)
#reverse_sentence(test_string)

test_string.must_equal "apples? them like you do How"
#test_string.must_equal "apples? them like you do How"
reverse_sentence(test_string).must_equal "apples? them like you do How"
end

it "reverse a sentence with preceeding and trailing white spaces" do
test_string = " I can do this! "

reverse_sentence(test_string)
#reverse_sentence(test_string)

test_string.must_equal " this! do can I "
#test_string.must_equal " this! do can I "
reverse_sentence(test_string).must_equal " this! do can I "
end
end
end
File renamed without changes.