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

Choose a reason for hiding this comment

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

Actually since start_position jumps to the end of the reversed word after a reversal, this is O(n)

# Space complexity: ? O(n) because of the empty_index_positions array from lines 33 - 38

Choose a reason for hiding this comment

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

I would say O(m) where m is the number of words, but yet. Could you instead do this with O(1) space complexity?


def swap_chars(arr, index_one, index_two)
tmp = arr[index_two]
arr[index_two] = arr[index_one]
arr[index_one] = tmp
end

def reverse_sub_str(array, start_position, end_position)
((end_position - start_position + 1) / 2).times do
swap_chars(array, start_position, end_position)
start_position += 1
end_position -= 1
end
return array
end

def reverse_sentence(my_sentence)
raise NotImplementedError

if my_sentence == ""
return ""
elsif my_sentence == nil
return nil
end


i = 0
j = (my_sentence.length) -1
reverse_sub_str(my_sentence, i, j)

empty_index_positions = []
my_sentence.length.times do |i|
if my_sentence[i] == " "
empty_index_positions << i
end
end

if empty_index_positions.length == 0
i = 0
j = (my_sentence.length) -1
reverse_sub_str(my_sentence, i, j)
return my_sentence
end

empty_index_positions.length.times do |i|
if i == 0
reverse_sub_str(my_sentence, i, empty_index_positions[i] - 1)
else
start_position = empty_index_positions[i - 1] + 1
end_position = empty_index_positions[i] - 1
reverse_sub_str(my_sentence, start_position, end_position)
end
end

i = empty_index_positions[-1] + 1
j = my_sentence.length - 1
reverse_sub_str(my_sentence, i, j )
return my_sentence
end
29 changes: 26 additions & 3 deletions lib/sort_by_length.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
# 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(n) because of splitting the string at the beginning of the method

Choose a reason for hiding this comment

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

yup


def swap(arr, index_one, index_two)
tmp = arr[index_two]
arr[index_two] = arr[index_one]
arr[index_one] = tmp
end


def sort_by_length(my_sentence)
raise NotImplementedError, "Method not implemented"
my_sentence = my_sentence.split

j = 0 # outer counter
my_sentence.length.times do
comparison_word = my_sentence[j]
for i in j...(my_sentence.length)
if my_sentence[i].length < comparison_word.length
puts my_sentence[i]
puts comparison_word
swap(my_sentence, i, j )
comparison_word = my_sentence[j]
end
Comment on lines +20 to +25

Choose a reason for hiding this comment

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

This is an interesting sort, it's not one of the standard sorts but does sort elements. It does work however.

end
j += 1
end
return my_sentence
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