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
35 changes: 33 additions & 2 deletions lib/reverse_sentence.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,37 @@
# A method to reverse the words in a sentence, in place.

# Time complexity: ?
# Space complexity: ?
def reverse_sentence(my_sentence)
raise NotImplementedError

# min = my_sentence[0]
# max = my_sentence[-1]
# my_arr = #["yoda"__"is"___"awesome!"].dup and add in spaces to new array[?]
# my_arr = ["yoda", "__", "is", "___", "awesome"]
# my_new_arr = []
# loop thru
# first = my_arr[0]
# last = my_arr[-1]
# first, last = last, first
# last += 1
# first -= -1
# shovel into my_new_array
# in a perfect world, would return in order we want

def reverse(sentence)

Choose a reason for hiding this comment

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

This method reverses a string, it doesn't reverse the words in a sentence. You're on the right track.

Consider:

  1. Reverse the string
  2. Loop through reversing each word in the sentence.

initial = 0
last = sentence.length - 1

while initial < last
memo = sentence[initial]
sentence[initial] = sentence[last]

sentence[last] = memo

initial += 1
last -= 1

return sentence

Choose a reason for hiding this comment

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

This return should be outside the while loop

end
end

puts reverse("y__i____c")
30 changes: 29 additions & 1 deletion lib/sort_by_length.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,34 @@
# sorted by the length of the word.
# Time complexity: ?
# Space complexity: ?


# Sky_array =[ The sky is blue ].split (“ “)
# => [“the”, “sky”, “is”, “blue"]
# sky_array = 3 3 2 4#character count
# find min, then find max
#range loop, min to max,
# Output = [ is the sky blue ]

sky_arr = "the sky is blue"
def sort_by_length(my_sentence)
raise NotImplementedError, "Method not implemented"
return nil if my_sentence == nil
final_arr = []
sky_arr = my_sentence.split(" ")
length_arr = sky_arr.map do |word|
word.length
end

min = length_arr.min
max = length_arr.max

(min..max).each do |length|
sky_arr.each do |word|
if word.length == length
final_arr << word
end
end
Comment on lines +26 to +31

Choose a reason for hiding this comment

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

This looks like it works, but it's a little inefficient. Instead you could just do a bubblesort using the string lengths.

end
return final_arr
end
puts sort_by_length(sky_arr)