From 97d2380154e568b8f84af825e3ee3d6e5cc2ff59 Mon Sep 17 00:00:00 2001 From: Amy Lee Date: Tue, 26 Sep 2017 02:17:55 -0700 Subject: [PATCH 1/2] Passing all tests, code is rough --- string_manipulation.rb | 52 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 46 insertions(+), 6 deletions(-) diff --git a/string_manipulation.rb b/string_manipulation.rb index c8b52e1..3f936fc 100644 --- a/string_manipulation.rb +++ b/string_manipulation.rb @@ -1,22 +1,62 @@ # A method to reverse a string in place. def string_reverse(my_string) - puts "NOT IMPLEMENTED" + #first, check if string has more than one letter + return my_string[0] if my_string.length <= 1 + i = 0 + j = my_string.length - 1 + while i < j + temp = my_string[i] + my_string[i]= my_string[j] + my_string[j]= temp + i+=1 + j-=1 + end + + return my_string + end # A method to reverse each word in a sentence, in place. def reverse_words(my_words) - puts "NOT IMPLEMENTED" + #check for more than one word, else, return the one word reversed + first = 0 + while first < my_words.length + first += 1 while my_words[first] == " " + last = first + 1 + + until my_words[last] == " " || last == my_words.length + last += 1 + end + word = my_words[first...last] + + my_words[first...last] = string_reverse(word) + + first = last + 1 + end + end # A method to reverse the words in a sentence, in place. def reverse_sentence(my_sentence) - puts "NOT IMPLEMENTED" + reversed_string = string_reverse(my_sentence) + + reverse_words(reversed_string) end # 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" + #if word is one letter long? + i = 0 + j = my_phrase.length - 1 + while i < j + i+=1 until my_phrase[i] != " " + j-=1 until my_phrase[j] != " " + return false if my_phrase[i] != my_phrase[j] + i+=1 + j-=1 + end + return true end @@ -67,8 +107,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 c8f0893afa6d83c5d63add71c57b48ff9bebf20e Mon Sep 17 00:00:00 2001 From: Amy Lee Date: Mon, 2 Oct 2017 00:33:18 -0700 Subject: [PATCH 2/2] Passing --- string_manipulation.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/string_manipulation.rb b/string_manipulation.rb index 3f936fc..ee1a392 100644 --- a/string_manipulation.rb +++ b/string_manipulation.rb @@ -1,6 +1,6 @@ # A method to reverse a string in place. def string_reverse(my_string) - #first, check if string has more than one letter + return my_string[0] if my_string.length <= 1 i = 0 j = my_string.length - 1 @@ -18,7 +18,6 @@ def string_reverse(my_string) # A method to reverse each word in a sentence, in place. def reverse_words(my_words) - #check for more than one word, else, return the one word reversed first = 0 while first < my_words.length first += 1 while my_words[first] == " " @@ -46,7 +45,6 @@ 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) - #if word is one letter long? i = 0 j = my_phrase.length - 1 while i < j