From b0b59265384e950d5d9e4a026dc96674842101fa Mon Sep 17 00:00:00 2001 From: Kimberley Zell Date: Sat, 30 Sep 2017 14:12:09 -0700 Subject: [PATCH 1/2] completed --- string_manipulation.rb | 274 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 247 insertions(+), 27 deletions(-) diff --git a/string_manipulation.rb b/string_manipulation.rb index c8b52e1..fccbd63 100644 --- a/string_manipulation.rb +++ b/string_manipulation.rb @@ -1,30 +1,242 @@ +require 'pry' # A method to reverse a string in place. def string_reverse(my_string) - puts "NOT IMPLEMENTED" + i = 0 + j = my_string.length-1 + hold = nil + + unless my_string.length <= 1 + while i < j + hold = my_string[i] + my_string[i] = my_string[j] + my_string[j] = hold + i += 1 + 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" + next_word_index = 0 + i = 0 + j = 0 #will equal the length of current word-1 + next_word_index = 0 + hold = nil + + until next_word_index >= my_words.length + + until my_words[j] == " " || my_words[j] == nil + j += 1 + end + + next_word_index = (j + 1) + j -= 1 + while i < j + hold = my_words[i] + my_words[i] = my_words[j] + my_words[j] = hold + i += 1 + j -= 1 + end + + i = next_word_index + j = next_word_index + end + + return my_words end +# def word_length(word, i, j) +# +# until word[j] == " " || word[j] == nil +# j += 1 +# end +# +# return j +# end + # A method to reverse the words in a sentence, in place. def reverse_sentence(my_sentence) - puts "NOT IMPLEMENTED" + #everything in between white space treat as a word? + +string_reverse(my_sentence) + +reverse_words(my_sentence) + end +# i = 0 +# j = 0 +# hold = nil +# r_low = 0 +# r_high = 0 +# +# until j >= my_sentence.length +# word_length = word_length(my_sentence, i, j) +# word_length.times do +# until i >= j +# hold = my_sentence[i] +# r_low = my_sentence.length-j +# my_sentence[i] = my_sentence[r_low] +# my_sentence[r_low] = hold +# i += 1 +# end +# end +# i += 1 +# j += 1 +# end +# +# return my_sentence + +##White space messed up +# l_low_index = 0 +# l_high_index = 0 +# +# r_low_index = my_sentence.length-1 +# r_high_index = my_sentence.length-1 +# hold = nil +# +# while l_high_index < r_low_index +# until my_sentence[l_high_index] == " " +# l_high_index += 1 +# end +# until my_sentence[r_low_index] == " " +# r_low_index -= 1 +# end +# +# hold = my_sentence[r_low_index+1..r_high_index] +# my_sentence[r_low_index+1..r_high_index] = my_sentence[l_low_index..l_high_index-1] +# +# my_sentence[l_low_index..l_high_index-1] = hold +# +# l_high_index += 1 +# l_low_index = l_high_index +# r_low_index -= 1 +# r_high_index = l_low_index +# +# +# end + + ## DOESN"T WORK + # left_word_low_index = 0 + # left_word_high_index = 0 + # + # right_word_low_index = my_sentence.length-1 + # right_word_high_index = my_sentence.length-1 + # + # until my_sentence[left_word_high_index] == " " || nil + # left_word_high_index += 1 + # end + # + # until my_sentence[right_word_low_index] == " " + # right_word_low_index -= 1 + # end + # + # left_word_high_index -= 1 + # right_word_low_index += 1 + + + +## DOESN"T WORK + # next_word_index = 0 + # i = 0 + # j = 0 #will equal the length of current word-1 + # next_word_index = 0 + # hold = nil + # + # until next_word_index >= my_sentence.length + # + # until my_sentence[j] == " " || my_sentence[j] == nil + # j += 1 + # end + # + # next_word_index = (j + 1) + # j -= 1 + # + # hold = my_sentence[i..j] + # right_low_index = (my_sentence.length-1)-hold.length + # right_high_index = my_sentence.length + # + # my_sentence[i..j] = my_sentence[right_low_index...right_high_index] + # + # my_sentence[right_low_index...right_high_index] = hold + # + # j = next_word_index + # i = next_word_index + # + # right_high_index = right_low_index - 1 + # right_low_index = right_high_index - hold.length + # # my_sentence[i..j] = my_sentence[((my_sentence.length-1)-hold.length)...my_sentence.length] + # + # + # end +# 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 + j = my_phrase.length-1 + + if j <= 1 + return false + end + + while i < j + if my_phrase[i] == " " + i += 1 + end + + if my_phrase[j] == " " + j -= 1 + end + + unless my_phrase[i] == my_phrase[j] + return false + end + i += 1 + j -= 1 + + end return true end # 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 + k = 0 + string_end = my_string.length + until k >= string_end + count = 0 + j = i+1 + + if my_string[j] == my_string[i] + k = j+1 + count += 2 + + while my_string[k] == my_string[i] && k <= string_end + count += 1 + my_string.slice!(k) + string_end -= 1 + end + + if count > 2 + my_string[j] = count.to_s + end + + else + k += 1 + end + i = k + end + + return my_string end ### ---- END OF METHODS @@ -50,6 +262,14 @@ def encode_repeating(my_string) puts "BUG! The reversed words should be '#{reversed_words}' and not '#{my_words}'" end +#find the last index of first word +#insert the word with the end of sentence and hold end of sentence letters +#go left until find a space +#take those letters and move to front of sentence +#take letters in hold and insert to the right + + + puts "Test 3: reversed sentence" sentence = "Yoda is awesome" puts "Original: #{sentence}" @@ -67,27 +287,27 @@ 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 -# 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." +##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." From eb7bb3d05561f869814d58c07f5dcfc03f0689ef Mon Sep 17 00:00:00 2001 From: Kimberley Zell Date: Sun, 1 Oct 2017 10:50:19 -0700 Subject: [PATCH 2/2] completed --- string_manipulation.rb | 116 +---------------------------------------- 1 file changed, 2 insertions(+), 114 deletions(-) diff --git a/string_manipulation.rb b/string_manipulation.rb index fccbd63..42fbeb7 100644 --- a/string_manipulation.rb +++ b/string_manipulation.rb @@ -48,14 +48,7 @@ def reverse_words(my_words) return my_words end -# def word_length(word, i, j) -# -# until word[j] == " " || word[j] == nil -# j += 1 -# end -# -# return j -# end + # A method to reverse the words in a sentence, in place. def reverse_sentence(my_sentence) @@ -66,112 +59,7 @@ def reverse_sentence(my_sentence) reverse_words(my_sentence) end -# i = 0 -# j = 0 -# hold = nil -# r_low = 0 -# r_high = 0 -# -# until j >= my_sentence.length -# word_length = word_length(my_sentence, i, j) -# word_length.times do -# until i >= j -# hold = my_sentence[i] -# r_low = my_sentence.length-j -# my_sentence[i] = my_sentence[r_low] -# my_sentence[r_low] = hold -# i += 1 -# end -# end -# i += 1 -# j += 1 -# end -# -# return my_sentence - -##White space messed up -# l_low_index = 0 -# l_high_index = 0 -# -# r_low_index = my_sentence.length-1 -# r_high_index = my_sentence.length-1 -# hold = nil -# -# while l_high_index < r_low_index -# until my_sentence[l_high_index] == " " -# l_high_index += 1 -# end -# until my_sentence[r_low_index] == " " -# r_low_index -= 1 -# end -# -# hold = my_sentence[r_low_index+1..r_high_index] -# my_sentence[r_low_index+1..r_high_index] = my_sentence[l_low_index..l_high_index-1] -# -# my_sentence[l_low_index..l_high_index-1] = hold -# -# l_high_index += 1 -# l_low_index = l_high_index -# r_low_index -= 1 -# r_high_index = l_low_index -# -# -# end - - ## DOESN"T WORK - # left_word_low_index = 0 - # left_word_high_index = 0 - # - # right_word_low_index = my_sentence.length-1 - # right_word_high_index = my_sentence.length-1 - # - # until my_sentence[left_word_high_index] == " " || nil - # left_word_high_index += 1 - # end - # - # until my_sentence[right_word_low_index] == " " - # right_word_low_index -= 1 - # end - # - # left_word_high_index -= 1 - # right_word_low_index += 1 - - - -## DOESN"T WORK - # next_word_index = 0 - # i = 0 - # j = 0 #will equal the length of current word-1 - # next_word_index = 0 - # hold = nil - # - # until next_word_index >= my_sentence.length - # - # until my_sentence[j] == " " || my_sentence[j] == nil - # j += 1 - # end - # - # next_word_index = (j + 1) - # j -= 1 - # - # hold = my_sentence[i..j] - # right_low_index = (my_sentence.length-1)-hold.length - # right_high_index = my_sentence.length - # - # my_sentence[i..j] = my_sentence[right_low_index...right_high_index] - # - # my_sentence[right_low_index...right_high_index] = hold - # - # j = next_word_index - # i = next_word_index - # - # right_high_index = right_low_index - 1 - # right_low_index = right_high_index - hold.length - # # my_sentence[i..j] = my_sentence[((my_sentence.length-1)-hold.length)...my_sentence.length] - # - # - # end -# end + # A method to check if the input string is a palindrome. # Return true if the string is a palindrome. Return false otherwise.