From c31165c6c5f1463ed8dc2070eaa215be17beca03 Mon Sep 17 00:00:00 2001 From: Kay Date: Mon, 17 Sep 2018 22:22:47 -0700 Subject: [PATCH 1/2] reverse words --- .DS_Store | Bin 0 -> 6148 bytes lib/reverse_words.rb | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0 Date: Mon, 24 Sep 2018 12:38:50 -0400 Subject: [PATCH 2/2] made changes to solution --- lib/reverse_words.rb | 53 ++++++++++++++++++++++++++++++++------------ 1 file changed, 39 insertions(+), 14 deletions(-) diff --git a/lib/reverse_words.rb b/lib/reverse_words.rb index 28761fb..b5f0153 100644 --- a/lib/reverse_words.rb +++ b/lib/reverse_words.rb @@ -1,25 +1,50 @@ # A method to reverse each word in a sentence, in place. +require 'pry' def string_reverse(my_string) return if my_string == nil || my_string.length < 1 - i = 0 #first ele - j = my_string.length - 1 #last ele - while i < j # until position of first ele meets last, so there is the center element in between - temp = my_string[i] #stores frist ele in temp - my_string[i] = my_string[j] #sets first ele to the last ele's position - my_string[j] = temp #sets last element to first ele's position - i += 1 #increment - j -= 1 - end - return my_string + i = 0 #first ele + j = my_string.length - 1 #last ele + while i < j # until position of first ele meets last, so there is the center element in between + temp = my_string[i] #stores frist ele in temp + my_string[i] = my_string[j] #sets first ele to the last ele's position + my_string[j] = temp #sets last element to first ele's position + i += 1 #increment + j -= 1 + end + return my_string end +# " evol " + def reverse_words(my_words) + # puts my_words unless my_words == nil || my_words.empty? - my_words. - string_reverse(my) + last = 0 + first = 0 + until last == my_words.length - 1 + while my_words[first] == " " && first != my_words.length - 1 + first += 1 + end + last = first + while my_words[last] != " " && last != my_words.length - 1 + last += 1 + end + if first == last || last == my_words.length - 1 + my_words[first..last] = string_reverse(my_words[first..last]) + else + my_words[first..last - 1] = string_reverse(my_words[first..last - 1]) + end + first = last + end + end + # puts "finished #{my_words}" end - - +#1, check if first character is " " +#check if the index to the right of it (the next highest index) is a space +#if true, that section is one word +# (the beginning and end of each word needs an index) +#once i find a since word section call string_reverse helper method on it +#else, continue incrementation +1 # "En gineer" # "reenig nE"