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

Dionisia Edges reverse_words #30

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
38 changes: 37 additions & 1 deletion lib/reverse_words.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,40 @@
# A method to reverse each word in a sentence, in place.
def reverse_words(my_words)
raise NotImplementedError
# raise NotImplementedError
return my_words if my_words.nil? || my_words.empty?

my_words_start = 0
my_words_end = 0
index = 0

until index > my_words.length
if my_words[index] != " " && my_words[index] != nil
index += 1

else
my_words_end = index -1
index += 1
while my_words_start < my_words_end
temp = my_words[my_words_start] # swap with temporary variable
my_words[my_words_start] = my_words[my_words_end]
my_words[my_words_end] = temp
my_words_start += 1
my_words_end -= 1
end
my_words_start = index
my_words_end = index
end
end

Choose a reason for hiding this comment

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

Thought for further improvement: With the last test case scenario " evol ", your algorithm does work because for the first 4 spaces, on line 17 my_words_start will end up being greater than my_words_end. Can you intentionally add consideration for preceding and trailing consecutive white spaces?

return my_words

Choose a reason for hiding this comment

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

Since you're manipulating my_words in place, you don't need to return my_words. Line 28 could simply be return.

end


# reversed_string = ''
#
# i = 0
# while i < my_words.length
# reversed_string = my_words[i] + reversed_string
# i += 1
# end
#
# return reversed_string