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

laura_reverse_words | Nodes #42

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
43 changes: 42 additions & 1 deletion lib/reverse_words.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,45 @@
require 'pry'
# A method to reverse each word in a sentence, in place.
def reverse_words(my_words)
raise NotImplementedError
if my_words == nil || my_words == "" || my_words.length == 1
return my_words
end
i = 0
while i < my_words.length
start_index = 0
end_index = 0
while my_words[i] == " "
i += 1
end
if my_words[i] != " "
start_index = i
end
# creates an infinitive loops
while my_words[i + 1] != " " && my_words[i + 1] != nil
i += 1
end
end_index = i
string_reverse(my_words, start_index, end_index)
i += 1
end
return
end

def string_reverse(my_string, start_index, end_index)
if my_string == nil || my_string == "" || my_string.length == 1
return my_string
else
i = start_index
j = end_index
while i < j
# swap in place
char = my_string[i]
my_string[i] = my_string[j]
my_string[j] = char

i += 1
j -= 1
end
end
return my_string
end
2 changes: 1 addition & 1 deletion specs/reverse_words_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
end
end

# check for edge cases
#check for edge cases
describe "edge cases" do
# if it's a string parameter, check for empty
it "reverse each word in an empty sentence" do
Expand Down