diff --git a/lib/reverse_sentence.rb b/lib/reverse_sentence.rb index 3fe7cac..b102b07 100644 --- a/lib/reverse_sentence.rb +++ b/lib/reverse_sentence.rb @@ -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) + 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 + end end + +puts reverse("y__i____c") diff --git a/lib/sort_by_length.rb b/lib/sort_by_length.rb index a5713ad..1342332 100644 --- a/lib/sort_by_length.rb +++ b/lib/sort_by_length.rb @@ -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 + end + return final_arr end +puts sort_by_length(sky_arr) \ No newline at end of file