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
23 changes: 20 additions & 3 deletions lib/reverse_sentence.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
# A method to reverse the words in a sentence, in place.
# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n) because the amount of time it will take will increase as the length of the input increases.
# Space complexity: O(n) because the space increases as the inout increases.

Choose a reason for hiding this comment

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

You were asked to try to do this in place which mean O(1) space complexity. Can you see a way?

def reverse_sentence(my_sentence)
raise NotImplementedError
words = my_sentence.split(" ")
length = words.length

mid = (length)/2
i = 0
k = 1
while i < mid do
j = words[length-k]
words[length-k] = words[i]
words[i] = j
i += 1
k += 1
end

if i == mid
my_sentence = words.join(", ")

Choose a reason for hiding this comment

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

Take a look at my how functions work lesson. Doing this doesn't affect the argument, which is why the tests fail.

return my_sentence
end
end
30 changes: 27 additions & 3 deletions lib/sort_by_length.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,31 @@
# 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) quadratice because there is a while loop with an if statemeny inside of it.
# Space complexity: O(n) linear because the array will increase the same size of the increas in size of the string.
def sort_by_length(my_sentence)
raise NotImplementedError, "Method not implemented"
if my_sentence.nil? || my_sentence == ""
return []
end
word_array = my_sentence.split(" ")
sorted = []
sorted << word_array[0]


word_array.each do |word|
index = 0
while index < word_array.length
if word.length < sorted[index].length
sorted.insert(index, word)
break
elsif word.length == sorted[index].length
sorted.insert(index+1,word)
break
elsif index == sorted.length-1
sorted.insert(index+1,word)
break
end
index += 1
Comment on lines +16 to +27

Choose a reason for hiding this comment

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

This is a very strange nonstandard sort. It works, but does a lot of extra inserts. Take a look at the textbook curriculum lesson on sorting and compare this to the standard sorts.

end
end
return sorted.uniq
end
5 changes: 3 additions & 2 deletions test/sort_by_length.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require "pry"
require_relative "test_helper"

describe "sort_by_length" do
Expand All @@ -8,11 +9,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