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

Reverse Words - Hannah #24

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
45 changes: 44 additions & 1 deletion lib/reverse_words.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,47 @@
require 'pry'
# A method to reverse each word in a sentence, in place.
def reverse_words(my_words)
raise NotImplementedError
#split string into array of words
return my_words if my_words.nil?

new_string = ""
var = ""

my_words.length.times do |x|
if my_words[x] != " "
var += my_words[x]
else
new_string += string_reverse(var)
new_string += my_words[x]
var = ""
end
end
new_string += string_reverse(var)

my_words.length.times do |x|
my_words[x] = new_string[x]
end
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 updating the characters in the input parameter my_words in place, you don't need to return anything. Line 24 could simply be return.

Choose a reason for hiding this comment

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

Ditto with line 42

end

def string_reverse(my_string)
return my_string if my_string.nil?

return my_string if my_string.length <= 1

i = 0
j = my_string.length - 1
b = 0
while i < j
b = my_string[i]
my_string[i] = my_string[j]
my_string[j] = b
i += 1
j -= 1
end
return my_string
end

# test_string = "I like tomatoes"
#
# print reverse_words(test_string)
3 changes: 2 additions & 1 deletion specs/reverse_words_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'minitest/autorun'
require 'minitest/reporters'
require_relative '../lib/reverse_words'
require 'pry'

describe "reverse words" do
describe "basic tests" do
Expand All @@ -16,7 +17,7 @@
test_string = "hello, world"

reverse_words(test_string)

# binding.pry
test_string.must_equal ",olleh dlrow"
end
end
Expand Down