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

Amber Lynn - Edges | reverse_words #37

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
55 changes: 54 additions & 1 deletion lib/reverse_words.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,57 @@
# A method to reverse each word in a sentence, in place.
# def string_reverse(my_string)
#
# if my_string == nil
# return nil
# elsif my_string.length == 1
# return my_string
# elsif
# my_string.length > 1
# temp = ""
# i = 0
# j = (my_string.length - 1)
# while i < j
# temp = my_string[i]
# my_string[i] = my_string[j]
# my_string[j] = temp
# i += 1
# j -= 1
# end
# return my_string
# end
# end


def reverse_words(my_words)
raise NotImplementedError
if my_words == nil || my_words.length == 0
return nil
else
start = 0
ender = 0

my_words.length.times do |i|
ender = i

Choose a reason for hiding this comment

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

To take care of multiple consecutive, white spaces, at the beginning of the times' loop you could check if my_words[start] is a white space. If it is, update, start to be i+1 and continue to the next iteration in the times' loop, otherwise, continue on with the rest of your logic.


if my_words[i] == " "
ender = i - 1
while start < ender
temp = my_words[ender]
my_words[ender] = my_words[start]
my_words[start] = temp
start += 1
ender -= 1
end
start = i + 1
end
end

while start < ender
temp = my_words[ender]
my_words[ender] = my_words[start]
my_words[start] = temp
start += 1
ender -= 1
end
end
return my_words
end