From a28b5140334cfa397a9164fa9be7457875473dcd Mon Sep 17 00:00:00 2001 From: Jocelyn Gonzalez Date: Tue, 26 Sep 2017 02:19:17 -0700 Subject: [PATCH 1/2] adjusting for encode repeating tests --- string_manipulation.rb | 99 ++++++++++++++++++++++++++++++++---------- 1 file changed, 75 insertions(+), 24 deletions(-) diff --git a/string_manipulation.rb b/string_manipulation.rb index c8b52e1..43a160c 100644 --- a/string_manipulation.rb +++ b/string_manipulation.rb @@ -1,22 +1,59 @@ # A method to reverse a string in place. def string_reverse(my_string) - puts "NOT IMPLEMENTED" + temp = "" + mid = my_string.length / 2 + i = 0 + + while i < mid + temp = my_string[i] + my_string[i] = my_string[my_string.length - 1 - i] + my_string[my_string.length - 1 - i] = temp + i += 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" + i = 0 + min = 0 + max = 0 + while i < my_words.length + until (my_words[i] == " ") || (i == my_words.length) + i += 1 + end + max = i + + while min < max - 1 + temp = my_words[min] + my_words[min] = my_words[max - 1] + my_words[max - 1] = temp + min += 1 + max -= 1 + end + i += 1 + min = i + end + return my_words end # A method to reverse the words in a sentence, in place. def reverse_sentence(my_sentence) - puts "NOT IMPLEMENTED" + reverse_words(my_sentence) + string_reverse(my_sentence) 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" + i=0 + while i < my_phrase.length + if my_phrase[i] != my_phrase[my_phrase.length - 1 - i] + return false + else + i += 1 + end + end return true end @@ -24,7 +61,21 @@ def palindrome_check(my_phrase) # with a number representing the frequncy. The replacement is done only if the # string length will get reduced by the process. def encode_repeating(my_string) - puts "NOT IMPLEMENTED" + i = 0 + min = 0 + max = 0 + temp = "" + while i < my_string.length + until (my_string[i] !=my_string[i+1]) + i += 1 + end + max = i + 1 + temp << my_string[min] + temp << "#{max - min}" + i += 1 + min = i + end + return temp end ### ---- END OF METHODS @@ -72,22 +123,22 @@ def encode_repeating(my_string) puts "Palindrome test complete." # Optional Question #5 -# puts "Test 5: Encode test" -# test1 = "aaabbbbbcccc" -# encode_repeating(test1) -# if test1 != "a3b5c4" -# puts "BUG! 'aaabbbbbcccc' should get encoded to 'a3b5c4', not '#{test1}'" -# end -# -# test2 = "xxxyttttgeee" -# encode_repeating(test2) -# if test2 != "x3yt4ge3" -# puts "BUG! 'xxxyttttgeee' should get encoded to 'x3yt4ge3', not '#{test2}'" -# end -# -# test3 = "ddbbfffgjjjj" -# encode_repeating(test3) -# if test3 != "ddbbf3gj4" -# puts "BUG! 'ddbbfffgjjjj' should get encoded to 'ddbbf3gj4', not '#{test3}'" -# end -# puts "Encode test complete." +puts "Test 5: Encode test" +test1 = "aaabbbbbcccc" +encode_repeating(test1) +if test1 != "a3b5c4" + puts "BUG! 'aaabbbbbcccc' should get encoded to 'a3b5c4', not '#{test1}'" +end + +test2 = "xxxyttttgeee" +encode_repeating(test2) +if test2 != "x3yt4ge3" + puts "BUG! 'xxxyttttgeee' should get encoded to 'x3yt4ge3', not '#{test2}'" +end + +test3 = "ddbbfffgjjjj" +encode_repeating(test3) +if test3 != "ddbbf3gj4" + puts "BUG! 'ddbbfffgjjjj' should get encoded to 'ddbbf3gj4', not '#{test3}'" +end +puts "Encode test complete." From 32cf8f8116b9e323fdbd3b0224f190d156df9830 Mon Sep 17 00:00:00 2001 From: Jocelyn Gonzalez Date: Fri, 29 Sep 2017 16:04:29 -0700 Subject: [PATCH 2/2] complete w/o bonus q's --- string_manipulation.rb | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/string_manipulation.rb b/string_manipulation.rb index 43a160c..75dbfa7 100644 --- a/string_manipulation.rb +++ b/string_manipulation.rb @@ -61,21 +61,23 @@ def palindrome_check(my_phrase) # with a number representing the frequncy. The replacement is done only if the # string length will get reduced by the process. def encode_repeating(my_string) - i = 0 - min = 0 - max = 0 - temp = "" - while i < my_string.length - until (my_string[i] !=my_string[i+1]) - i += 1 - end - max = i + 1 - temp << my_string[min] - temp << "#{max - min}" - i += 1 - min = i - end - return temp + puts "NOT IMPLEMENTED" + # i = 0 + # min = 0 + # max = 0 + # temp = "" + # while i < my_string.length + # until (my_string[i] !=my_string[i+1]) + # i += 1 + # end + # max = i + 1 + # temp << my_string[min] + # temp << "#{max - min}" + # i += 1 + # min = i + # end + # my_string = temp + # return my_string end ### ---- END OF METHODS