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
67 changes: 64 additions & 3 deletions lib/reverse_sentence.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,67 @@

# A method to reverse the words in a sentence, in place.
# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n) because the algorithm becuase the time complexity will change as the size of the input changes
# Space complexity: O(1) because the same number of variables will be made regardless of input size
def reverse_sentence(my_sentence)
raise NotImplementedError

return my_sentence if my_sentence == nil

counter = 0
word_length = 0
index = 0

word_start = 0
# needs to be an outer loop that iterates thru the sentence
while counter < my_sentence.length

word_length = 0

while my_sentence[counter] != " " && counter < my_sentence.length
# whatever index is when the loop exits, is where the word ends
word_length += 1
counter += 1
end

word_end = counter - 1

while my_sentence[counter] == " "
counter += 1
end

# this is going to swap each letter in each word
word_reverse(my_sentence, word_start, word_end)

word_start = counter
end

i = 0

until i == my_sentence.length/2
temp = my_sentence[i]
my_sentence[i] = my_sentence[my_sentence.length - i - 1]
my_sentence[my_sentence.length - i - 1] = temp
i += 1
end
Comment on lines +39 to +44

Choose a reason for hiding this comment

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

You could also use the word_reverse method again to reverse the whole string.


return my_sentence

end

def word_reverse(my_sentence, word_start, word_end)
word_length = word_end - word_start + 1

counter = 0

while counter != word_length/2
temp = my_sentence[word_start]
my_sentence[word_start] = my_sentence[word_end]
my_sentence[word_end] = temp

word_start += 1
word_end -= 1
counter += 1
end
end

Comment on lines +50 to +65

Choose a reason for hiding this comment

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

I love this helper method! 👍



27 changes: 24 additions & 3 deletions lib/sort_by_length.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
# 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) because as the algorithm scales, each nested loop will run n number of times according to the input.
# Space complexity: O(1) because the same amount of memory will be used regardless of the input size.

Choose a reason for hiding this comment

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

Technically since you are doing .split, you have O(n) space complexity.

def sort_by_length(my_sentence)

Choose a reason for hiding this comment

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

Nice SmartBubble sort!

raise NotImplementedError, "Method not implemented"
sentence_array = my_sentence.split

i = 0
swapped = true

while i < sentence_array.length - 1 && swapped
j = 0
swapped = false
while j < sentence_array.length - i - 1
if sentence_array[j].length > sentence_array[j + 1].length
temp = sentence_array[j]
sentence_array[j] = sentence_array[j + 1]
sentence_array[j + 1] = temp
swapped = true
end
j += 1
end
i += 1
end
return sentence_array
end


File renamed without changes.