From 731ea3c0488fc575d0cc0a453be8fbfaf8105a39 Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Fri, 22 Sep 2017 14:20:27 -0700 Subject: [PATCH 1/5] Finish String Reverse --- string_manipulation.rb | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/string_manipulation.rb b/string_manipulation.rb index c8b52e1..aaa2c3c 100644 --- a/string_manipulation.rb +++ b/string_manipulation.rb @@ -1,9 +1,20 @@ # A method to reverse a string in place. def string_reverse(my_string) - puts "NOT IMPLEMENTED" + i = 0 + j = my_string.length - 1 + reversed_string = my_string + while i < (my_string.length / 2) + temp = reversed_string[i] + reversed_string[i] = reversed_string[j] + reversed_string[j] = temp + i += 1 + j -= 1 + end + return reversed_string end -# A method to reverse each word in a sentence, in place. + +# aA method to reverse each word in a sentence, in place. def reverse_words(my_words) puts "NOT IMPLEMENTED" end From f8bf4066ac755a724e9545d89d63eb860ca0c711 Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Tue, 26 Sep 2017 12:15:18 -0700 Subject: [PATCH 2/5] Reverse Words Done --- string_manipulation.rb | 85 ++++++++++++++++++++++++++---------------- 1 file changed, 52 insertions(+), 33 deletions(-) diff --git a/string_manipulation.rb b/string_manipulation.rb index aaa2c3c..673e13c 100644 --- a/string_manipulation.rb +++ b/string_manipulation.rb @@ -13,10 +13,29 @@ def string_reverse(my_string) return reversed_string end - +# puts "Test 2: reversed words" +# my_words = "I can be an engineer" +# puts "Original: #{my_words}" +# reversed_words = "I nac eb na reenigne" +# reverse_words(my_words) # aA method to reverse each word in a sentence, in place. def reverse_words(my_words) - puts "NOT IMPLEMENTED" + i = 0 + start = 0 + segment = "" + + while i < my_words.length + if my_words[i] == " " + my_words[start..(i-1)] = string_reverse(segment) + segment = "" + start = i + 1 + else + segment << my_words[i] + end #if statement + i += 1 + end #while statement + my_words[start..(i-1)] = string_reverse(segment) + return my_words end # A method to reverse the words in a sentence, in place. @@ -39,16 +58,16 @@ def encode_repeating(my_string) end ### ---- END OF METHODS -puts "Test 1: reverse a string" -my_string = "Lovelace" -puts "Original string: #{my_string}" -reversed_string = "ecalevoL" -string_reverse(my_string) -if my_string == reversed_string - puts "String reversed correctly. Reversed string: #{reversed_string}" -else - puts "BUG! The reversed string should be '#{reversed_string}' and not '#{my_string}'" -end +# puts "Test 1: reverse a string" +# my_string = "Lovelace" +# puts "Original string: #{my_string}" +# reversed_string = "ecalevoL" +# string_reverse(my_string) +# if my_string == reversed_string +# puts "String reversed correctly. Reversed string: #{reversed_string}" +# else +# puts "BUG! The reversed string should be '#{reversed_string}' and not '#{my_string}'" +# end puts "Test 2: reversed words" my_words = "I can be an engineer" @@ -60,27 +79,27 @@ def encode_repeating(my_string) else puts "BUG! The reversed words should be '#{reversed_words}' and not '#{my_words}'" end - -puts "Test 3: reversed sentence" -sentence = "Yoda is awesome" -puts "Original: #{sentence}" -reversed_sentence = "awesome is Yoda" -reverse_sentence(sentence) -if sentence == reversed_sentence - puts "Sentence reversed correctly. Reversed sentence: '#{reversed_sentence}'" -else - puts "BUG! The reversed sentence should be '#{reversed_sentence}' and not '#{sentence}'" -end - -puts "Test 4: Palindrome check" -phrase = "madam" -puts "BUG: madam is a palindrome and should return true" if palindrome_check(phrase) != true -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 -puts "Palindrome test complete." +# +# puts "Test 3: reversed sentence" +# sentence = "Yoda is awesome" +# puts "Original: #{sentence}" +# reversed_sentence = "awesome is Yoda" +# reverse_sentence(sentence) +# if sentence == reversed_sentence +# puts "Sentence reversed correctly. Reversed sentence: '#{reversed_sentence}'" +# else +# puts "BUG! The reversed sentence should be '#{reversed_sentence}' and not '#{sentence}'" +# end +# +# puts "Test 4: Palindrome check" +# phrase = "madam" +# puts "BUG: madam is a palindrome and should return true" if palindrome_check(phrase) != true +# 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 +# puts "Palindrome test complete." # Optional Question #5 # puts "Test 5: Encode test" From 11d5eee0d251341e14bcf6dda87715366e5b24e3 Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Wed, 27 Sep 2017 18:10:54 -0700 Subject: [PATCH 3/5] finish reverse_sentence --- string_manipulation.rb | 63 +++++++++++++++++++++++------------------- 1 file changed, 34 insertions(+), 29 deletions(-) diff --git a/string_manipulation.rb b/string_manipulation.rb index 673e13c..b9de5d3 100644 --- a/string_manipulation.rb +++ b/string_manipulation.rb @@ -13,17 +13,16 @@ def string_reverse(my_string) return reversed_string end -# puts "Test 2: reversed words" -# my_words = "I can be an engineer" -# puts "Original: #{my_words}" -# reversed_words = "I nac eb na reenigne" -# reverse_words(my_words) -# aA method to reverse each word in a sentence, in place. def reverse_words(my_words) i = 0 start = 0 segment = "" + until my_words[i] != " " + i += 1 + start += 1 + end + while i < my_words.length if my_words[i] == " " my_words[start..(i-1)] = string_reverse(segment) @@ -38,9 +37,14 @@ def reverse_words(my_words) return my_words end + +# sentence = "Yoda is awesome" +# puts "Original: #{sentence}" +# reversed_sentence = "awesome is Yoda" # A method to reverse the words in a sentence, in place. def reverse_sentence(my_sentence) - puts "NOT IMPLEMENTED" + string_reverse(my_sentence) + return reverse_words(my_sentence) end # A method to check if the input string is a palindrome. @@ -58,16 +62,16 @@ def encode_repeating(my_string) end ### ---- END OF METHODS -# puts "Test 1: reverse a string" -# my_string = "Lovelace" -# puts "Original string: #{my_string}" -# reversed_string = "ecalevoL" -# string_reverse(my_string) -# if my_string == reversed_string -# puts "String reversed correctly. Reversed string: #{reversed_string}" -# else -# puts "BUG! The reversed string should be '#{reversed_string}' and not '#{my_string}'" -# end +puts "Test 1: reverse a string" +my_string = "Lovelace" +puts "Original string: #{my_string}" +reversed_string = "ecalevoL" +string_reverse(my_string) +if my_string == reversed_string + puts "String reversed correctly. Reversed string: #{reversed_string}" +else + puts "BUG! The reversed string should be '#{reversed_string}' and not '#{my_string}'" +end puts "Test 2: reversed words" my_words = "I can be an engineer" @@ -79,18 +83,19 @@ def encode_repeating(my_string) else puts "BUG! The reversed words should be '#{reversed_words}' and not '#{my_words}'" end -# -# puts "Test 3: reversed sentence" -# sentence = "Yoda is awesome" -# puts "Original: #{sentence}" -# reversed_sentence = "awesome is Yoda" -# reverse_sentence(sentence) -# if sentence == reversed_sentence -# puts "Sentence reversed correctly. Reversed sentence: '#{reversed_sentence}'" -# else -# puts "BUG! The reversed sentence should be '#{reversed_sentence}' and not '#{sentence}'" -# end -# + + +puts "Test 3: reversed sentence" +sentence = "Yoda is awesome" +puts "Original: #{sentence}" +reversed_sentence = "awesome is Yoda" +reverse_sentence(sentence) +if sentence == reversed_sentence + puts "Sentence reversed correctly. Reversed sentence: '#{reversed_sentence}'" +else + puts "BUG! The reversed sentence should be '#{reversed_sentence}' and not '#{sentence}'" +end + # puts "Test 4: Palindrome check" # phrase = "madam" # puts "BUG: madam is a palindrome and should return true" if palindrome_check(phrase) != true From b1ef3593a0bbebc909f932335083da20de3d7a3a Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Wed, 27 Sep 2017 18:42:03 -0700 Subject: [PATCH 4/5] finish palindromes --- string_manipulation.rb | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/string_manipulation.rb b/string_manipulation.rb index b9de5d3..f4536e7 100644 --- a/string_manipulation.rb +++ b/string_manipulation.rb @@ -50,9 +50,26 @@ 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" + i = 0 + j = my_phrase.length - 1 + + until i >= j + if my_phrase[i] == " " + i += 1 + elsif my_phrase[j] == " " + j -= 1 + end #first if statement + + if my_phrase[i] == my_phrase[j] + i += 1 + j -= 1 + else + return false + end #second if statement + end #until return true -end +end #method + # A method that updates the string by replacing consecutive repeating characters # with a number representing the frequncy. The replacement is done only if the @@ -96,15 +113,15 @@ def encode_repeating(my_string) puts "BUG! The reversed sentence should be '#{reversed_sentence}' and not '#{sentence}'" end -# puts "Test 4: Palindrome check" -# phrase = "madam" -# puts "BUG: madam is a palindrome and should return true" if palindrome_check(phrase) != true -# 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 -# puts "Palindrome test complete." +puts "Test 4: Palindrome check" +phrase = "madam" +puts "BUG: madam is a palindrome and should return true" if palindrome_check(phrase) != true +phrase = "empty" +puts "BUG: empty is not a palindrome and should return false" if palindrome_check(phrase) != false +puts "optional challenge" +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 # puts "Test 5: Encode test" From 080a242f2526a0e307dfaa5f0e13cbb540c883f1 Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Sat, 30 Sep 2017 13:38:51 -0700 Subject: [PATCH 5/5] Finish all tests --- string_manipulation.rb | 69 ++++++++++++++++++++++++++++++------------ 1 file changed, 49 insertions(+), 20 deletions(-) diff --git a/string_manipulation.rb b/string_manipulation.rb index f4536e7..b7d41e3 100644 --- a/string_manipulation.rb +++ b/string_manipulation.rb @@ -18,6 +18,7 @@ def reverse_words(my_words) start = 0 segment = "" + #check for space at start until my_words[i] != " " i += 1 start += 1 @@ -74,8 +75,36 @@ def palindrome_check(my_phrase) # A method that updates the string by replacing consecutive repeating characters # 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 + j = 0 + + until i == my_string.length + + letter_count = 1 + + until my_string[j] != my_string[j + 1] + letter_count += 1 + j += 1 + end #end until + + if letter_count > 2 + my_string[i + 1] = (letter_count).to_s + my_string.slice!((i + 2)..(i + (letter_count - 1))) + i += 2 + j = i + elsif letter_count == 2 + i += 2 + j = i + else + i += 1 + j = i + end + + end #end until + + return my_string end ### ---- END OF METHODS @@ -124,22 +153,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."