From 2bf5e2e7c9661ba156b7dd572c055b0bbef5cc5c Mon Sep 17 00:00:00 2001 From: kee nam Date: Fri, 22 Sep 2017 14:46:17 -0700 Subject: [PATCH 1/5] Finished string_reverse(my_string) --- string_manipulation.rb | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/string_manipulation.rb b/string_manipulation.rb index c8b52e1..e8c17eb 100644 --- a/string_manipulation.rb +++ b/string_manipulation.rb @@ -1,6 +1,21 @@ # A method to reverse a string in place. def string_reverse(my_string) - puts "NOT IMPLEMENTED" + length = my_string.length + if length == 0 + return nil + elsif length == 1 + return my_string + else + i = 0 + j = length - 1 + while i < j + original = my_string[i] + my_string[i] = my_string[j] + my_string[j] = original + i += 1 + j -= 1 + end + end end # A method to reverse each word in a sentence, in place. From 74348e44b7dac5c86372cd36b8c85ec025a491d6 Mon Sep 17 00:00:00 2001 From: kee nam Date: Thu, 28 Sep 2017 14:39:46 -0700 Subject: [PATCH 2/5] Added reverse_words method --- string_manipulation.rb | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/string_manipulation.rb b/string_manipulation.rb index e8c17eb..f5b3821 100644 --- a/string_manipulation.rb +++ b/string_manipulation.rb @@ -1,9 +1,8 @@ +require 'awesome_print' # A method to reverse a string in place. def string_reverse(my_string) length = my_string.length - if length == 0 - return nil - elsif length == 1 + if length <= 1 return my_string else i = 0 @@ -16,11 +15,31 @@ def string_reverse(my_string) j -= 1 end end + return my_string end # A method to reverse each word in a sentence, in place. def reverse_words(my_words) - puts "NOT IMPLEMENTED" + length = my_words.length + if length <= 1 + return my_words + else + whitespaces = (0...length).find_all { |j| my_words[j,1] == ' ' } + i = 0 + until i == whitespaces.length + min = (whitespaces[i-1]) + 1 + max = whitespaces[i] + if min > max + min = max + end + reverse = string_reverse(my_words[min...max]) + my_words[min...max] = reverse + i += 1 + end + last = (whitespaces.last) + 1 + my_words[last...my_words.length] = string_reverse(my_words[last...my_words.length]) + end + return my_words end # A method to reverse the words in a sentence, in place. From 0ab33c0b3a26ee214dcc5ed4f01669ac1a1b5e1c Mon Sep 17 00:00:00 2001 From: kee nam Date: Thu, 28 Sep 2017 14:48:39 -0700 Subject: [PATCH 3/5] Edited reverse_words method and added reverse_sentence method --- string_manipulation.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/string_manipulation.rb b/string_manipulation.rb index f5b3821..d4689e2 100644 --- a/string_manipulation.rb +++ b/string_manipulation.rb @@ -29,7 +29,9 @@ def reverse_words(my_words) until i == whitespaces.length min = (whitespaces[i-1]) + 1 max = whitespaces[i] - if min > max + if i == 0 && max > 1 + min = 0 + elsif min > max min = max end reverse = string_reverse(my_words[min...max]) @@ -44,7 +46,8 @@ def reverse_words(my_words) # A method to reverse the words in a sentence, in place. def reverse_sentence(my_sentence) - puts "NOT IMPLEMENTED" + string_reverse(my_sentence) + reverse_words(my_sentence) end # A method to check if the input string is a palindrome. From 04142529e51069d13eb181b88339b6b4989317cf Mon Sep 17 00:00:00 2001 From: kee nam Date: Thu, 28 Sep 2017 14:59:10 -0700 Subject: [PATCH 4/5] Added palindrom_check method --- string_manipulation.rb | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/string_manipulation.rb b/string_manipulation.rb index d4689e2..4c3d45b 100644 --- a/string_manipulation.rb +++ b/string_manipulation.rb @@ -53,8 +53,18 @@ def reverse_sentence(my_sentence) # A method to check if the input string is a palindrome. # Return true if the string is a palindrome. Return false otherwise. def palindrome_check(my_phrase) - puts "NOT IMPLEMENTED" - return true + length = my_phrase.length + whitespaces = (0...length).find_all { |j| my_phrase[j,1] == ' ' } + for space in whitespaces + my_phrase[space] = '' + end + first_half = my_phrase[0...my_phrase.length/2] + second_half = my_phrase[(my_phrase.length/2 + 1)..my_phrase.length] + if string_reverse(first_half) == second_half + return true + else + return false + end end # A method that updates the string by replacing consecutive repeating characters @@ -104,8 +114,8 @@ def encode_repeating(my_string) phrase = "empty" puts "BUG: empty is not a palindrome and should return false" if palindrome_check(phrase) != false # optional challenge -# phrase = "nurses run" -# puts "BUG: 'nurses run' is a palindrome and should return true" if palindrome_check(phrase) != true +phrase = "nurses run" +puts "BUG: 'nurses run' is a palindrome and should return true" if palindrome_check(phrase) != true puts "Palindrome test complete." # Optional Question #5 From 1d1ca76cde05cb2395a07adc759ca5b2fa80a7de Mon Sep 17 00:00:00 2001 From: kee nam Date: Thu, 28 Sep 2017 21:22:26 -0700 Subject: [PATCH 5/5] Added conditional to palindrome_check method --- string_manipulation.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/string_manipulation.rb b/string_manipulation.rb index 4c3d45b..567d9c1 100644 --- a/string_manipulation.rb +++ b/string_manipulation.rb @@ -59,12 +59,12 @@ def palindrome_check(my_phrase) my_phrase[space] = '' end first_half = my_phrase[0...my_phrase.length/2] - second_half = my_phrase[(my_phrase.length/2 + 1)..my_phrase.length] - if string_reverse(first_half) == second_half - return true + if my_phrase.length % 2 == 0 + second_half = my_phrase[(my_phrase.length/2)..my_phrase.length] else - return false + second_half = my_phrase[(my_phrase.length/2 + 1)..my_phrase.length] end + string_reverse(first_half) == second_half ? true : false end # A method that updates the string by replacing consecutive repeating characters