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
52 changes: 48 additions & 4 deletions lib/reverse_sentence.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,50 @@
# A method to reverse the words in a sentence, in place.
# Time complexity: ?
# Space complexity: ?
def reverse_sentence(my_sentence)
raise NotImplementedError
# Time complexity: O(n + m) -

Choose a reason for hiding this comment

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

You're reversing the whole string = n
Then you go through and reverse each word for a total of < n letters reversed
So O(2n) = O(n)

# Reverse_Sentence
# - 1st while loop: O(.5n) where n is the number of letters
# - 2nd while loop: O(n) where n is the number of letters
# Reverse
# - O(m): where m is the number of words
# Space complexity: O(1)

def reverse(text, text_start, text_end)
i = 0
((text_end - text_start)/2).times do
temp_char = text[text_start + i]
text[text_start + i] = text[text_end - i - 1]
text[text_end - i - 1] = temp_char
i += 1
end
end
Comment on lines +10 to 18

Choose a reason for hiding this comment

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

Great helper method.

Could you have used it in other places in the main method?



def reverse_sentence(my_sentence)
return nil if my_sentence.nil?
letter = 0
length = my_sentence.length

end_char = length - 1
while letter < my_sentence.length/2 do
temp_char = my_sentence[letter]
my_sentence[letter] = my_sentence[length - 1 - letter]
my_sentence[length - 1 - letter] = temp_char
letter += 1
end
Comment on lines +27 to +32

Choose a reason for hiding this comment

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

Hmm... another reversal section


letter = 0
while letter < my_sentence.length do
while my_sentence[letter] == " "
letter += 1
end
start_index = letter

while my_sentence[letter] != " " && letter < my_sentence.length
letter += 1
end
end_index = letter

reverse(my_sentence, start_index, end_index)
end

return my_sentence
end
23 changes: 19 additions & 4 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)
# Space complexity: O(n)
def sort_by_length(my_sentence)
raise NotImplementedError, "Method not implemented"
end
return nil if my_sentence.nil?

my_sorted_array = []
my_array = my_sentence.split

until my_array.length == 0 do
current_word = my_array[0]
my_array.each do |word|
if word.length < current_word.length
current_word = word
end
end
my_sorted_array.push(current_word)
my_array.delete(current_word)

Choose a reason for hiding this comment

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

Just note .delete is an O(n) operation.

end
return my_sorted_array
end
File renamed without changes.