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
87 changes: 84 additions & 3 deletions lib/reverse_sentence.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,87 @@
# 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 the string, my_sentence. We iterate through the entire sentence but when a "word" is reached we iterate through that word, swapping each letter.
# Space complexity: O(1) because nothing new is created, everything is swapped/reversed in place.

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

# #Take in string and convert to array of words with spaces perseved
# sentence_array = my_sentence.split(/\s/)

# #Iterate through array swapping each words
# i = 0
# j = sentence_array.length - 1
# while i < j
# temp_index = sentence_array[i]
# sentence_array[i] = sentence_array[j]
# sentence_array[j] = temp_index

# i += 1
# j -= 1
# end

# #Concatente elements of array into a new string output using (.join)
# new_sentence = sentence_array.join(' ')

# #iterate through new string and replace each character into parameter, my_sentence
# new_sentence.length.times do |i|
# my_sentence[i] = new_sentence[i]
# end

# return my_sentence

############ Second Try at Problem #############

# Step 1 - Reverse sentence in its entirity
# Create helper method to do this
def reverse(string)
start_point = 0
end_point = string.length - 1
while start_point < end_point
temp = string[start_point]
string[start_point] = string[end_point]
string[end_point] = temp
start_point += 1
end_point -= 1
end
return string
end

# Pass in my_sentence into Step 1 helper method
reverse(my_sentence)

# Step 2 Identify 'words' in sentence
# Identify start and end positions of each word
i = 0
start_point = 0
while i < my_sentence.length
if my_sentence[i] == " "
end_point = i - 1

while start_point < end_point
temp = my_sentence[start_point]
my_sentence[start_point] = my_sentence[end_point]
my_sentence[end_point] = temp
start_point += 1
end_point -= 1
end
start_point = i + 1

elsif i == my_sentence.length - 1
end_point = i
while start_point < end_point
temp = my_sentence[start_point]
my_sentence[start_point] = my_sentence[end_point]
my_sentence[end_point] = temp
start_point += 1
end_point -= 1
end

end
i += 1
end
return my_sentence
end
34 changes: 31 additions & 3 deletions lib/sort_by_length.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,35 @@
# 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 the array where the array consists of the words in the sentence as the elements of the array.
# Space complexity: O(n) because a new array is created from the words that make up the sentence string.
def sort_by_length(my_sentence)
raise NotImplementedError, "Method not implemented"
if my_sentence.length == nil
return []
end

# split string sentence into array (took this from my Exercism assignment)
my_sentence = my_sentence.split(/[^\w']+/)

# iterate through my_sentence array
# if word.length at index > word.length at index+1
# swap words
# else keep words in place
# iterate to next word
# end

i = 0
while i < (my_sentence.length - 1)
j = 0
while j < my_sentence.length - i - 1
if (my_sentence[j].length) > (my_sentence[j+1]).length
temp = my_sentence[j]
my_sentence[j] = my_sentence[j+1]
my_sentence[j+1] = temp
end
j += 1
end
i += 1
end

return my_sentence
end
20 changes: 10 additions & 10 deletions test/reverse_sentence_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
test_string = "hello, world"

reverse_sentence(test_string)

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)

test_string.must_equal "awesome! is Yoda"
_(test_string).must_equal "awesome! is Yoda"
end
end

Expand All @@ -27,7 +27,7 @@

reverse_sentence(test_string)

test_string.must_be_empty
_(test_string).must_be_empty
end

# if the parameter is an object, check for nil
Expand All @@ -36,39 +36,39 @@

reverse_sentence(test_string)

test_string.must_be_nil
_(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"
_(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"
_(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"
_(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 "
_(test_string).must_equal " this! do can I "
end
end
end
File renamed without changes.
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'minitest/autorun'
require 'minitest/reporters'
require 'minitest/skip_dsl'
require_relative '../lib/reverse_sentence'

Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new
Expand Down