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
72 changes: 69 additions & 3 deletions lib/reverse_sentence.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,72 @@
# A method to reverse the words in a sentence, in place.
# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n) - my method does not have any nested loops, but it will run through the length of the original string multiple times
# Space complexity: O(n) - a second variable (reverse) needs to be created, which will take up as much space as the original string

Choose a reason for hiding this comment

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

You were asked to reverse the sentence in place. So think if you can do this with O(1) space complexity.

def reverse_sentence(my_sentence)
raise NotImplementedError
if my_sentence != nil
# Creates array of words and spaces
words_array = words(my_sentence)

# Revereses the order of the words
length = words_array.length
i = 0
j = length - 1
(length / 2).times do
# selects word at smallest index that has not been swapped
word_1 = words_array[i]
# selects words at largest index that has not been swapped
word_2 = words_array[j]

words_array[i] = word_2
words_array[j] = word_1

i += 1
j -= 1
end

reverse = words_array.join

# Overrides original string with order of characters in reverse
i = 0
my_sentence.length.times do
my_sentence[i] = reverse[i]
i += 1
end
end

return my_sentence
end

# Helper method to convert sentence into array of words - O(n) time complexity
def words(my_sentence)
length = my_sentence.length
words_array = []
i = 0
word = ""

until i == length
l = my_sentence[i]
j = my_sentence[i + 1]

if l != " " && j != " "
word += l
elsif l != " " && j == " "
word += l
words_array << word
word = ""
elsif l == " " && j == " "
word += l
elsif l == " " && j != " "
word += l
words_array << word
word = ""
end

if i == length - 1
words_array << word
end

i += 1
end

return words_array
end
20 changes: 17 additions & 3 deletions lib/sort_by_length.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
# 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)

Choose a reason for hiding this comment

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

Correct for a basic bubble sort.

# Space complexity: O(n)
def sort_by_length(my_sentence)
raise NotImplementedError, "Method not implemented"
new_sentence = my_sentence.split(" ")
length = new_sentence.length
i = 0

(length - 1).times do
j = new_sentence[i]
if new_sentence[i].length > new_sentence[i + 1].length
new_sentence[i] = new_sentence[i + 1]
new_sentence[i + 1] = j
end

i += 1
end

return new_sentence
end
2 changes: 1 addition & 1 deletion test/sort_by_length.rb → test/sort_by_length_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
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
end