Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Katrina - Edges - Reverse Sentence #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
61 changes: 61 additions & 0 deletions lib/reverse_sentence.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,65 @@
# A method to reverse the words in a sentence, in place.
def reverse_sentence(my_sentence)
return if my_sentence == nil || my_sentence == ''

i = 0
end_of_s = my_sentence.length - 1

# reverse the whole sentence
reverse_string(my_sentence, i, end_of_s)

# reverse the words
reverse_words(my_sentence)

return
raise NotImplementedError

Choose a reason for hiding this comment

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

Since there is a return statement on line 14, line 15 is dead code, meaning that it will never get executed. Hence you can safely delete line 15.

end


def reverse_words(my_words)
i = 0
all_words_end = my_words.length

while i < all_words_end

while my_words[i] == ' ' && i < all_words_end
i += 1
end
start_of_word = i

while my_words[i] != ' ' && i < all_words_end
i += 1
end
end_of_word = i - 1

reverse_string(my_words, start_of_word, end_of_word)
end

return my_words
raise NotImplementedError
end

def reverse_string(string, start_of_word, end_of_word)

i = start_of_word
j = end_of_word

while i < j
temp = string[i]
string[i] = string[j]
string[j] = temp

i += 1
j -= 1
end

return string
end


=begin
Time complexity is linear O(3n), where n is the length of the input(my_sentence)

Space complexity is constant O(1), because the storage remains the same
even if the input (my_sentece) increases.
=end