From b5fca9648b20d559a2ff86174201fbdace107d23 Mon Sep 17 00:00:00 2001 From: Maryam Shitu Date: Mon, 17 Sep 2018 20:34:34 -0700 Subject: [PATCH 1/4] almost solved --- lib/reverse_words.rb | 65 +++++++++++++++++++++++++++++++++++-- specs/reverse_words_spec.rb | 3 +- 2 files changed, 65 insertions(+), 3 deletions(-) diff --git a/lib/reverse_words.rb b/lib/reverse_words.rb index cb3cc8a..402a50b 100644 --- a/lib/reverse_words.rb +++ b/lib/reverse_words.rb @@ -1,4 +1,65 @@ -# A method to reverse each word in a sentence, in place. +# # A method to reverse each word in a sentence, in place. +require 'pry' + + +# def reverse_words(my_words) +# if my_words == nil +# return nil +# end +# +# i = 0 +# j = my_words.length - 1 +# +# while i < j +# first_letter = my_words[i] +# last_letter = my_words[j] +# +# my_words[i] = last_letter +# my_words[j] = first_letter +# +# i += 1 +# j -= 1 +# end +# +# end + +################################ +def reverse_word(word) + i = 0 + j = word.length - 1 + + while i < j + first_letter = word[i] + last_letter = word[j] + + word[i] = last_letter + word[j] = first_letter + + i += 1 + j -= 1 + end + + return word +end + + def reverse_words(my_words) - raise NotImplementedError + + i = 0 + j = my_words.length + + while i < j + if my_words[i] == " " + + while i < count + + end + + reversed_words << reverse_word(word) + end + + count += 1 + end + + return 1my_words end diff --git a/specs/reverse_words_spec.rb b/specs/reverse_words_spec.rb index 2db8644..ec6ad4d 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 @@ -8,7 +9,7 @@ test_string = "Engineer" reverse_words(test_string) - + test_string.must_equal "reenignE" end From 246825b1488c8f17eeffc5b7715221c9d1fce844 Mon Sep 17 00:00:00 2001 From: Maryam Shitu Date: Mon, 17 Sep 2018 22:14:42 -0700 Subject: [PATCH 2/4] no longer close to working --- lib/reverse_words.rb | 45 ++++++++++--------- specs/reverse_words_spec.rb | 89 +++++++++++++++++++------------------ 2 files changed, 69 insertions(+), 65 deletions(-) diff --git a/lib/reverse_words.rb b/lib/reverse_words.rb index 402a50b..01c78c1 100644 --- a/lib/reverse_words.rb +++ b/lib/reverse_words.rb @@ -24,19 +24,19 @@ # end ################################ -def reverse_word(word) - i = 0 - j = word.length - 1 - while i < j +def reverse_one_word(word, index) + + i = 0 + while i < index first_letter = word[i] - last_letter = word[j] + last_letter = word[index] word[i] = last_letter - word[j] = first_letter + word[index] = first_letter i += 1 - j -= 1 + index -= 1 end return word @@ -44,22 +44,25 @@ def reverse_word(word) def reverse_words(my_words) + if my_words.nil? + return nil + end - i = 0 - j = my_words.length - - while i < j - if my_words[i] == " " - - while i < count - - end + k = 0 + l = my_words.length - reversed_words << reverse_word(word) - end + reverse_one_word(my_words, l) - count += 1 - end + # while i < j + # if my_words[i] == " " && i != 0 + # reverse_one_word(my_words, i) + # while (i + 1) < j + # reverse_one_word(my_words, j) + # end + # else + # reverse_one_word(my_words, j) + # end + # i += 1 + # end - return 1my_words end diff --git a/specs/reverse_words_spec.rb b/specs/reverse_words_spec.rb index ec6ad4d..b9984bb 100644 --- a/specs/reverse_words_spec.rb +++ b/specs/reverse_words_spec.rb @@ -9,7 +9,7 @@ test_string = "Engineer" reverse_words(test_string) - + test_string.must_equal "reenignE" end @@ -23,47 +23,48 @@ end # 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 - test_string = "" - - reverse_words(test_string) - - test_string.must_be_empty - end - - # if the parameter is an object, check for nil - it "nil object passed to reverse words" do - test_string = nil - - reverse_words(test_string) - - test_string.must_be_nil - end - - it "reverse each single character word in a sentence with multiple words" do - test_string = "a b c d e f g h ? 1 2 4" - - reverse_words(test_string) - - test_string.must_equal "a b c d e f g h ? 1 2 4" - end - - it "reverse each word in a sentence with multiple words of varying lengths" do - test_string = "I strive to engineer the most efficient solutions." - - reverse_words(test_string) - - test_string.must_equal "I evirts ot reenigne eht tsom tneiciffe .snoitulos" - end - - it "reverse words in a sentence with preceeding and trailing white spaces" do - test_string = " evol " - - reverse_words(test_string) - - test_string.must_equal " love " - end - end + # describe "edge cases" do + # + # # if it's a string parameter, check for empty + # it "reverse each word in an empty sentence" do + # test_string = "" + # + # reverse_words(test_string) + # + # test_string.must_be_empty + # end + # + # # if the parameter is an object, check for nil + # it "nil object passed to reverse words" do + # test_string = nil + # + # reverse_words(test_string) + # + # test_string.must_be_nil + # end + # + # it "reverse each single character word in a sentence with multiple words" do + # test_string = "a b c d e f g h ? 1 2 4" + # + # reverse_words(test_string) + # + # test_string.must_equal "a b c d e f g h ? 1 2 4" + # end + # + # it "reverse each word in a sentence with multiple words of varying lengths" do + # test_string = "I strive to engineer the most efficient solutions." + # + # reverse_words(test_string) + # + # test_string.must_equal "I evirts ot reenigne eht tsom tneiciffe .snoitulos" + # end + # + # it "reverse words in a sentence with preceeding and trailing white spaces" do + # test_string = " evol " + # + # reverse_words(test_string) + # + # test_string.must_equal " love " + # end + # end end From 488bafec4531145753d1415745bdcdf0d9b8b0c7 Mon Sep 17 00:00:00 2001 From: Maryam Shitu Date: Mon, 17 Sep 2018 22:24:54 -0700 Subject: [PATCH 3/4] errors --- lib/reverse_words.rb | 61 ++++++++----------------- specs/reverse_words_spec.rb | 88 ++++++++++++++++++------------------- 2 files changed, 63 insertions(+), 86 deletions(-) diff --git a/lib/reverse_words.rb b/lib/reverse_words.rb index 01c78c1..5aa7a90 100644 --- a/lib/reverse_words.rb +++ b/lib/reverse_words.rb @@ -1,31 +1,10 @@ # # A method to reverse each word in a sentence, in place. require 'pry' - -# def reverse_words(my_words) -# if my_words == nil -# return nil -# end -# -# i = 0 -# j = my_words.length - 1 -# -# while i < j -# first_letter = my_words[i] -# last_letter = my_words[j] -# -# my_words[i] = last_letter -# my_words[j] = first_letter -# -# i += 1 -# j -= 1 -# end -# -# end - -################################ - def reverse_one_word(word, index) + if word == nil + return nil + end i = 0 while i < index @@ -42,27 +21,25 @@ def reverse_one_word(word, index) return word end - def reverse_words(my_words) - if my_words.nil? + if my_words == nil return nil end - k = 0 - l = my_words.length - - reverse_one_word(my_words, l) - - # while i < j - # if my_words[i] == " " && i != 0 - # reverse_one_word(my_words, i) - # while (i + 1) < j - # reverse_one_word(my_words, j) - # end - # else - # reverse_one_word(my_words, j) - # end - # i += 1 - # end + z = 0 + y = my_words.length + + + while z < y + if my_words[z] == " " && z != 0 + reverse_one_word(my_words, y) + while (z + 1) < y + reverse_one_word(my_words, y) + end + else + reverse_one_word(my_words, y) + end + z += 1 + end end diff --git a/specs/reverse_words_spec.rb b/specs/reverse_words_spec.rb index b9984bb..b68bdad 100644 --- a/specs/reverse_words_spec.rb +++ b/specs/reverse_words_spec.rb @@ -23,48 +23,48 @@ end # 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 - # test_string = "" - # - # reverse_words(test_string) - # - # test_string.must_be_empty - # end - # - # # if the parameter is an object, check for nil - # it "nil object passed to reverse words" do - # test_string = nil - # - # reverse_words(test_string) - # - # test_string.must_be_nil - # end - # - # it "reverse each single character word in a sentence with multiple words" do - # test_string = "a b c d e f g h ? 1 2 4" - # - # reverse_words(test_string) - # - # test_string.must_equal "a b c d e f g h ? 1 2 4" - # end - # - # it "reverse each word in a sentence with multiple words of varying lengths" do - # test_string = "I strive to engineer the most efficient solutions." - # - # reverse_words(test_string) - # - # test_string.must_equal "I evirts ot reenigne eht tsom tneiciffe .snoitulos" - # end - # - # it "reverse words in a sentence with preceeding and trailing white spaces" do - # test_string = " evol " - # - # reverse_words(test_string) - # - # test_string.must_equal " love " - # end - # end + describe "edge cases" do + + # if it's a string parameter, check for empty + it "reverse each word in an empty sentence" do + test_string = "" + + reverse_words(test_string) + + test_string.must_be_empty + end + + # if the parameter is an object, check for nil + it "nil object passed to reverse words" do + test_string = nil + + reverse_words(test_string) + + test_string.must_be_nil + end + + it "reverse each single character word in a sentence with multiple words" do + test_string = "a b c d e f g h ? 1 2 4" + + reverse_words(test_string) + + test_string.must_equal "a b c d e f g h ? 1 2 4" + end + + it "reverse each word in a sentence with multiple words of varying lengths" do + test_string = "I strive to engineer the most efficient solutions." + + reverse_words(test_string) + + test_string.must_equal "I evirts ot reenigne eht tsom tneiciffe .snoitulos" + end + + it "reverse words in a sentence with preceeding and trailing white spaces" do + test_string = " evol " + + reverse_words(test_string) + + test_string.must_equal " love " + end + end end From 4f9b634a47df0d8105bc846728727b6a959c602c Mon Sep 17 00:00:00 2001 From: Maryam Shitu Date: Tue, 18 Sep 2018 23:39:51 -0700 Subject: [PATCH 4/4] tests passing --- lib/reverse_words.rb | 48 +++++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/lib/reverse_words.rb b/lib/reverse_words.rb index 5aa7a90..e0498cd 100644 --- a/lib/reverse_words.rb +++ b/lib/reverse_words.rb @@ -1,12 +1,14 @@ # # A method to reverse each word in a sentence, in place. require 'pry' -def reverse_one_word(word, index) +def reverse_one_word(word, start = 0, finish = word.length) if word == nil - return nil + return nil end - i = 0 + i = start + index = finish + while i < index first_letter = word[i] last_letter = word[index] @@ -23,23 +25,37 @@ def reverse_one_word(word, index) def reverse_words(my_words) if my_words == nil - return nil + return nil end - z = 0 - y = my_words.length + current_index = 0 + + start = nil + finish = nil + while current_index < my_words.length + currently_on_letter = my_words[current_index] != " " + currently_on_space = my_words[current_index] == " " + at_end_of_string = current_index == my_words.length - 1 - while z < y - if my_words[z] == " " && z != 0 - reverse_one_word(my_words, y) - while (z + 1) < y - reverse_one_word(my_words, y) - end - else - reverse_one_word(my_words, y) - end - z += 1 + if start == nil && currently_on_letter + start = current_index end + if start && currently_on_space + finish = current_index - 1 + end + + if start && at_end_of_string + finish = current_index + end + + if start && finish + reverse_one_word(my_words, start, finish) + start = nil + finish = nil + end + + current_index += 1 + end end