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

if my_sentence == nil
return nil
elsif
my_sentence == ""
return ""
end

array = my_sentence.split(/(\S*|\s*)/) # Split by each word and each space

Choose a reason for hiding this comment

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

This is making another array causing O(n) space complexity. You were asked to do the reverse in place.

i = 0

array.each do |match|
position = -i + my_sentence.length - match.length

my_sentence[position...position+match.length] = match
i += match.length
end
end
24 changes: 21 additions & 3 deletions lib/sort_by_length.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
# 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)
Comment on lines +3 to +4

Choose a reason for hiding this comment

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

Correct


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 bubblesort

raise NotImplementedError, "Method not implemented"
array = my_sentence.split
length = array.length
i = 0

while i < length-1
j = 0
while j < length-i-1
if array[j].length > array[j+1].length # compares the length of each word
temp = array[j]
array[j] = array[j+1]
array[j+1] = temp
end
j += 1
end
i += 1
end

return array
end
File renamed without changes.