Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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
30 changes: 30 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
GEM
remote: http://rubygems.org/
specs:
ansi (1.5.0)
builder (3.2.3)
minitest (5.10.3)
minitest-reporters (1.3.8)
ansi
builder
minitest (>= 5.0)
ruby-progressbar
minitest-skip (0.0.1)
minitest (~> 5.0)
rake (12.3.0)
ruby-progressbar (1.10.1)

PLATFORMS
ruby

DEPENDENCIES
minitest
minitest-reporters
minitest-skip
rake

RUBY VERSION
ruby 2.5.1p57

BUNDLED WITH
1.17.3
75 changes: 71 additions & 4 deletions lib/reverse_sentence.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,73 @@
# A method to reverse the words in a sentence, in place.
# Time complexity: ?
# Space complexity: ?
def reverse_sentence(my_sentence)
raise NotImplementedError
# Time complexity: O(n2)
# Space complexity: O(n)

Choose a reason for hiding this comment

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

Correct where n is the number of words.

def reverse_sentence(my_sentence)
#Step_0: Screen out all sentences that doesn't need to be/can't be reversed
if my_sentence == nil || my_sentence == "" || my_sentence.include?("\s") == false
return my_sentence
exit
end

#Step_1: Reverse all characters within the string in place
sentence_pairs = pairs_helper(my_sentence.length)
i = 1
(sentence_pairs).times do
temp = my_sentence[i-1]
my_sentence[i-1] = my_sentence[-i]
my_sentence[-i] = temp
i += 1

Choose a reason for hiding this comment

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

Notice that you're reversing a couple of times in this method. Could you instead make a helper method. That would dry this out.

end

#Step_2: Find all whitespace index(es) within the current string
space_indexes = (0...my_sentence.length).find_all { |c| my_sentence[c] == " " }

#Step_3: Reverse all characters for each word based on the whitespace index(es)
#Step_3.1: Reverse the first word in the string
word_pairs = pairs_helper(space_indexes[0])
i = 1
(word_pairs).times do
temp = my_sentence[i-1]
my_sentence[i-1] = my_sentence[space_indexes[0]-i]
my_sentence[space_indexes[0]-i] = temp
i += 1
end
#Step_3.2: reverse the last word in the string
word_pairs = pairs_helper((my_sentence.length - 1) - space_indexes[-1])
i = 1
(word_pairs).times do
temp = my_sentence[-i]
my_sentence[-i] = my_sentence[space_indexes[-1]+i]
my_sentence[space_indexes[-1]+i] = temp
i += 1
end
#Step_3.3: reverse the words in between (if any)
i = 0
if space_indexes.length == 1
return my_sentence
else
until i == (space_indexes.length - 1)
if my_sentence[space_indexes[i]+1] != " "
word_pairs = pairs_helper(space_indexes[i+1]-space_indexes[i]-1)
n = 1
(word_pairs).times do
temp = my_sentence[space_indexes[i]+n]
my_sentence[space_indexes[i]+n] = my_sentence[space_indexes[i+1]-n]
my_sentence[space_indexes[i+1]-n] = temp
n += 1
end
end
i += 1
end
return my_sentence
end
end

# Helper method to determin pairs of character that needs to be swapped
def pairs_helper(length)
if length % 2 == 0
pairs = length / 2
else
pairs = (length - 1) / 2
end
return pairs

Choose a reason for hiding this comment

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

I don't think this method is really needed as dividing an odd length by 2 will automatically round down.

end
27 changes: 22 additions & 5 deletions lib/sort_by_length.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
# A method which will return an array of the words in the string
# sorted by the length of the word.
# Time complexity: ?
# Space complexity: ?
# sorted by the length of the word.
# Time complexity: O(n2)
# Space complexity: O(n)

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"
end
#Step_1: break the sentence into an array of words
my_array = my_sentence.split(/\W+/)
#Step_2: compare the words and sort the array
i = 0
while i < my_array.length-1
j = 0
while j < my_array.length-i-1
if my_array[j].length > my_array[j+1].length
temp = my_array[j]
my_array[j] = my_array[j+1]
my_array[j+1] = temp
end
j += 1
end
i += 1
end
return my_array
end
File renamed without changes.