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
136 changes: 133 additions & 3 deletions lib/reverse_sentence.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,136 @@
# A method to reverse the words in a sentence, in place.
# Time complexity: ?
# Space complexity: ?
# Time complexity: O(2n) The array is reviewed twice, once for spaces to populate the space array, and once more piecewise to do all the swaps

Choose a reason for hiding this comment

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

So really O(n)

# Space complexity: O(1)

Choose a reason for hiding this comment

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

You are building a space array, so this would actually be: O(m) where m is the number of words.


require'pry'

def reverse_sentence(my_sentence)
raise NotImplementedError

# short circuit if empty sentence
if my_sentence == nil || !my_sentence.include?(" ")
return my_sentence
end

# determines the last index number that will be handled
last_index = my_sentence.length - 1

# reverses all the characters in the string
i = 0
my_sentence[i]
half_swap_limit = last_index / 2 + 1

until i == half_swap_limit
my_sentence[i], my_sentence[last_index - i] = my_sentence[last_index - i], my_sentence[i]

i += 1
Comment on lines +22 to +25

Choose a reason for hiding this comment

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

Notice that you reverse things a few times, this could be dryed up with a helper method.

end

# creates and populates an array of all space characters
space_array = []
i = 0
my_sentence.each_char do |character|
if character == " "
space_array << i
i += 1
else
i += 1
end
end



# determination of which methods are needed
if !space_array.include? (0)
first(space_array[0], my_sentence)
end

if !space_array.include? (last_index)
last(space_array[-1], last_index, my_sentence)
end

if space_array.length > 1 # this is run if there's more than 2 words, hence if there's more than 1 space
i = 0
p = space_array.length - 1

if i + 1 == p # short-circuit for 1-letter words
first_character = space_array[0] + 1
second_character = space_array[1] - 1
my_sentence[first_character], my_sentence[second_character] = my_sentence[second_character], my_sentence[first_character]
else
until i + 1 == p
middle(space_array[i], space_array[i+1], my_sentence)
i += 1
end
end
end
return my_sentence

end

Choose a reason for hiding this comment

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

These methods seem pretty repetitive. Could they be condensed into one method?

def first(first_space_index_number, my_sentence)
j = 0

last_index_to_swap = first_space_index_number - 1
half_limit = last_index_to_swap / 2

if last_index_to_swap.even?
until j == half_limit
my_sentence[last_index_to_swap - j], my_sentence[j] = my_sentence[j], my_sentence[last_index_to_swap - j]
j += 1
end
else
until j == half_limit + 1
my_sentence[last_index_to_swap - j], my_sentence[j] = my_sentence[j], my_sentence[last_index_to_swap - j]
j += 1
end
end
end

def last(last_space_index_number, last_index, my_sentence)

first_index_to_swap = last_space_index_number + 1
half_limit = (last_index - first_index_to_swap) / 2

j = 0

if (last_index - first_index_to_swap).even?
until j == half_limit
my_sentence[first_index_to_swap + j], my_sentence[last_index - j] = my_sentence[last_index - j], my_sentence[first_index_to_swap + j]
j += 1
end
else
until j == half_limit + 1
my_sentence[first_index_to_swap + j], my_sentence[last_index - j] = my_sentence[last_index - j], my_sentence[first_index_to_swap + j]
j += 1
end
end
end

def middle(first_space_index_number, last_space_index_number, my_sentence)
# short-circuit for one-letter words
if last_space_index_number - first_space_index_number == 1
return
elsif last_space_index_number - first_space_index_number == 3
my_sentence[first_space_index_number + 1], my_sentence[last_space_index_number - 1] = my_sentence[last_space_index_number - 1], my_sentence[first_space_index_number + 1]
return
end

first_index_to_swap = first_space_index_number + 1
last_index = last_space_index_number - 1
half_limit = (last_index - first_index_to_swap) / 2

j = 0

if (last_space_index_number - first_space_index_number).even?
until j == half_limit
my_sentence[first_index_to_swap + j], my_sentence[last_index - j] = my_sentence[last_index - j], my_sentence[first_index_to_swap + j]
j += 1
end
else
until j == half_limit + 1
my_sentence[first_index_to_swap + j], my_sentence[last_index - j] = my_sentence[last_index - j], my_sentence[first_index_to_swap + j]
j += 1
end

end
end
41 changes: 38 additions & 3 deletions lib/sort_by_length.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,42 @@
# 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(n2)
# Space complexity: O(1)

Choose a reason for hiding this comment

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

Technically since you're doing .split, this should be O(n)


def sort_by_length(my_sentence)
raise NotImplementedError, "Method not implemented"
# splits the sentence
array_of_sentence = my_sentence.split

#short-cirtcuit retunn statement for empty strings
if array_of_sentence.length == 0
return []
end

i = 0 #internal counter

e = 1 #external counter

# bubble sort :)
until e == array_of_sentence.length - 1

until i == array_of_sentence.length - 1

if array_of_sentence[i].length > array_of_sentence[i + 1].length
array_of_sentence[i], array_of_sentence[i + 1] = array_of_sentence[i + 1], array_of_sentence[i]
i += 1

Choose a reason for hiding this comment

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

You could move the i += 1 to after the if statement and drop the else.

else
i += 1
end

end

e += 1

end

#returns transformed, digested, sorted array
return_statement = array_of_sentence

return return_statement

end
4 changes: 2 additions & 2 deletions test/sort_by_length.rb → test/sort_by_length_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
it "will return an array of words, by length" do
expect(sort_by_length("I love Ada")).must_equal ["I", "Ada", "love"]
end

it "will return an array of words by length, words that are of equal length will appear in the order they appear" do
expect(sort_by_length("words of equal length")).must_equal ["of", "words", "equal", "length"]
end

it "will return an array of words by length, words that are of equal length will appear in the order they appear" do
expect(sort_by_length("I love great awesome words")).must_equal ["I", "love", "great", "words", "awesome"]
end
Expand Down