From 45422d284db98bb545202143a44c667903b31454 Mon Sep 17 00:00:00 2001 From: Hannah Date: Sun, 16 Sep 2018 22:52:47 -0700 Subject: [PATCH 1/3] complete but 4 tests not passing --- lib/reverse_words.rb | 45 ++++++++++++++++++++++++++++++++++++- specs/reverse_words_spec.rb | 3 ++- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/lib/reverse_words.rb b/lib/reverse_words.rb index cb3cc8a..a1bf690 100644 --- a/lib/reverse_words.rb +++ b/lib/reverse_words.rb @@ -1,4 +1,47 @@ # 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? + + separated_words = Array.new + var = "" + my_words.length.times do |x| + if my_words[x] != " " + var += my_words[x] + else + separated_words << var + separated_words << my_words[x] + var = "" + end + end + separated_words << var + + reversed_string = "" + separated_words.each do |word| + reversed_string += string_reverse(word) + end + my_words = reversed_string + return my_words 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 = "hello, world" + +print reverse_words(test_string) diff --git a/specs/reverse_words_spec.rb b/specs/reverse_words_spec.rb index 2db8644..1d41501 100644 --- a/specs/reverse_words_spec.rb +++ b/specs/reverse_words_spec.rb @@ -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 @@ -16,7 +17,7 @@ test_string = "hello, world" reverse_words(test_string) - + test_string.must_equal ",olleh dlrow" end end From a90d62f080aa15ea1f949d1da8cc97fa0e270306 Mon Sep 17 00:00:00 2001 From: Hannah Date: Tue, 18 Sep 2018 15:52:11 -0700 Subject: [PATCH 2/3] words reverse complete --- lib/reverse_words.rb | 24 ++++++++++++------------ specs/reverse_words_spec.rb | 2 +- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/reverse_words.rb b/lib/reverse_words.rb index a1bf690..e1b8510 100644 --- a/lib/reverse_words.rb +++ b/lib/reverse_words.rb @@ -1,27 +1,27 @@ +require 'pry' # A method to reverse each word in a sentence, in place. def reverse_words(my_words) #split string into array of words return my_words if my_words.nil? - separated_words = Array.new + new_string = "" var = "" + my_words.length.times do |x| if my_words[x] != " " var += my_words[x] else - separated_words << var - separated_words << my_words[x] + new_string += string_reverse(var) + new_string += my_words[x] var = "" end end - separated_words << var + new_string += string_reverse(var) - reversed_string = "" - separated_words.each do |word| - reversed_string += string_reverse(word) + my_words.length.times do |x| + my_words[x] = new_string[x] end - my_words = reversed_string - return my_words + end def string_reverse(my_string) @@ -42,6 +42,6 @@ def string_reverse(my_string) return my_string end -test_string = "hello, world" - -print reverse_words(test_string) +# test_string = "I like tomatoes" +# +# print reverse_words(test_string) diff --git a/specs/reverse_words_spec.rb b/specs/reverse_words_spec.rb index 1d41501..944b71d 100644 --- a/specs/reverse_words_spec.rb +++ b/specs/reverse_words_spec.rb @@ -17,7 +17,7 @@ test_string = "hello, world" reverse_words(test_string) - + # binding.pry test_string.must_equal ",olleh dlrow" end end From 40cf93ef9fbd0c72b86e2c139f1dd7354f3c46fb Mon Sep 17 00:00:00 2001 From: Hannah Date: Tue, 18 Sep 2018 20:49:55 -0700 Subject: [PATCH 3/3] added return --- lib/reverse_words.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/reverse_words.rb b/lib/reverse_words.rb index e1b8510..6f33a2e 100644 --- a/lib/reverse_words.rb +++ b/lib/reverse_words.rb @@ -21,7 +21,7 @@ def reverse_words(my_words) my_words.length.times do |x| my_words[x] = new_string[x] end - + return my_words end def string_reverse(my_string)