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
Binary file added .DS_Store
Binary file not shown.
29 changes: 25 additions & 4 deletions lib/reverse_sentence.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
# A method to reverse the words in a sentence, in place.
# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n^2) where n is the length of string
# Space complexity: O(n) where n is the length of string

Choose a reason for hiding this comment

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

Since you're not making a new array, this is actually O(1).


def reverse_sentence(my_sentence)
raise NotImplementedError
end
if my_sentence == nil
return nil
end

char_count = 0
position_count = my_sentence.length + 1

while char_count < my_sentence.length
if my_sentence[0] != " "
my_sentence.insert(position_count - 1, my_sentence[0])

Choose a reason for hiding this comment

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

Just a note, that insert is an O(n) operation. Could you do this reverse_sentence in O(n) time complexity?

my_sentence[0] = ""
char_count += 1
else
position_count = my_sentence.length-char_count
my_sentence.insert(position_count, " ")
my_sentence[0] = ""
char_count += 1
end
end

return my_sentence
end
21 changes: 18 additions & 3 deletions lib/sort_by_length.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
# 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) where n is the length of array
# Space complexity: O(n) where n is length of string/array
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 with accurate complexity

raise NotImplementedError, "Method not implemented"
# using bubble sort
sentence_in_array = my_sentence.split(" ")
i = 0
while i < sentence_in_array.length-1
j = 0
while j < sentence_in_array.length-i-1
if sentence_in_array[j].length > sentence_in_array[j+1].length
temp = sentence_in_array[j]
sentence_in_array[j] = sentence_in_array[j+1]
sentence_in_array[j+1] = temp
end
j += 1
end
i += 1
end
return sentence_in_array
end
46 changes: 23 additions & 23 deletions test/reverse_sentence_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,70 +4,70 @@
describe "basic tests" do
it "reverse a sentence with two words" do
test_string = "hello, world"

reverse_sentence(test_string)

test_string.must_equal "world hello,"
end

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

reverse_sentence(test_string)

test_string.must_equal "awesome! is Yoda"
end
end

# 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
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)

test_string.must_be_nil
end

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

reverse_sentence(test_string)

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)

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)

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)

test_string.must_equal " this! do can I "
end
end
Expand Down
4 changes: 2 additions & 2 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,11 +8,11 @@
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
Expand Down