From 78e2107b6528c066ab667f92c89382deec2446b8 Mon Sep 17 00:00:00 2001 From: Shaunna Date: Fri, 29 Sep 2017 10:55:22 -0700 Subject: [PATCH 1/3] all methods attempted --- string_manipulation.rb | 58 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 52 insertions(+), 6 deletions(-) diff --git a/string_manipulation.rb b/string_manipulation.rb index c8b52e1..c5096da 100644 --- a/string_manipulation.rb +++ b/string_manipulation.rb @@ -1,22 +1,68 @@ + + # A method to reverse a string in place. def string_reverse(my_string) - puts "NOT IMPLEMENTED" + 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" + i = 0 + j = 0 + k = 0 + + while k < my_words.length + 1 + if my_words[k] == " " || my_words[k] == nil + j = k - 1 + + while i < j + temp = my_words[i] + my_words[i] = my_words[j] + my_words[j] = temp + i += 1 + j -= 1 + end + i = k + 1 + end + k += 1 + end + return my_words end # A method to reverse the words in a sentence, in place. def reverse_sentence(my_sentence) - puts "NOT IMPLEMENTED" + return reverse_words(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 + j = my_phrase.length - 1 + while i < j + while my_phrase[i] == " " + i += 1 + end + while my_phrase[j] == " " + + j -= 1 + end + if my_phrase[i] != my_phrase[j] + return false + else + i += 1 + j -= 1 + end + end return true end @@ -67,8 +113,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 a6fcae328c487748c71aeaa146eda757e890b491 Mon Sep 17 00:00:00 2001 From: Shaunna Date: Fri, 29 Sep 2017 11:04:08 -0700 Subject: [PATCH 2/3] pull request questions completed --- .github/PULL_REQUEST_TEMPLATE | 16 ++++++++-------- string_manipulation.rb | 1 - 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/.github/PULL_REQUEST_TEMPLATE b/.github/PULL_REQUEST_TEMPLATE index 3284a71..b5d6845 100644 --- a/.github/PULL_REQUEST_TEMPLATE +++ b/.github/PULL_REQUEST_TEMPLATE @@ -6,13 +6,13 @@ What is the time and space complexity for each method you implemented? Provide j | Question | Answer | |--- |--- | -| What is the time complexity of the string_reverse method? Provide justification. | | -| What is the space complexity of the string_reverse method? Provide justification. | | -| What is the time complexity of the reverse_words method? Provide justification. | | -| What is the space complexity of the reverse_words method? Provide justification. | | -| What is the time complexity of the reverse_sentence method? Provide justification. | | -| What is the space complexity of the reverse_sentence method? Provide justification. | | -| What is the time complexity of the palindrome_check method? Provide justification. | | -| What is the space complexity of the palindrome_check method? Provide justification. | | +| What is the time complexity of the string_reverse method? Provide justification. | Linear. Each character in the string has to be used once. | +| What is the space complexity of the string_reverse method? Provide justification. | Constant. Only two variables are used regardless of the length of the string. | +| What is the time complexity of the reverse_words method? Provide justification. | Linear, each character must be looked at once. | +| What is the space complexity of the reverse_words method? Provide justification. | Constant, only three variables are used regardless of length of the string. | +| What is the time complexity of the reverse_sentence method? Provide justification. | Linear. While each method that is used inside is linear, the two are added together which is still linear. | +| What is the space complexity of the reverse_sentence method? Provide justification. | Constant, 3 variables are used total regardless of string length. | +| What is the time complexity of the palindrome_check method? Provide justification. | Linear, each character in the string is looked at once. | +| What is the space complexity of the palindrome_check method? Provide justification. | Constant. Only 2 variables are used regardless of length. | | What is the time complexity of the encode_repeating method? Provide justification. | | | What is the space complexity of the encode_repeating method? Provide justification. | | diff --git a/string_manipulation.rb b/string_manipulation.rb index c5096da..aa0d047 100644 --- a/string_manipulation.rb +++ b/string_manipulation.rb @@ -53,7 +53,6 @@ def palindrome_check(my_phrase) i += 1 end while my_phrase[j] == " " - j -= 1 end if my_phrase[i] != my_phrase[j] From a8f3d2cdbf4a3da8ee4c3bf54c38f702fa48ab41 Mon Sep 17 00:00:00 2001 From: Shaunna Date: Sun, 8 Oct 2017 09:54:03 -0700 Subject: [PATCH 3/3] finished encoding method --- string_manipulation.rb | 124 +++++++++++++++++++++++++++++++---------- 1 file changed, 95 insertions(+), 29 deletions(-) diff --git a/string_manipulation.rb b/string_manipulation.rb index aa0d047..afd0d88 100644 --- a/string_manipulation.rb +++ b/string_manipulation.rb @@ -1,7 +1,8 @@ - +require 'pry' # A method to reverse a string in place. def string_reverse(my_string) + i = 0 j = my_string.length - 1 while i < j @@ -14,23 +15,26 @@ def string_reverse(my_string) return my_string end + # A method to reverse each word in a sentence, in place. def reverse_words(my_words) + i = 0 j = 0 k = 0 - while k < my_words.length + 1 + while k <= my_words.length if my_words[k] == " " || my_words[k] == nil j = k - 1 - while i < j - temp = my_words[i] - my_words[i] = my_words[j] - my_words[j] = temp - i += 1 - j -= 1 - end + my_words[i..j] = reverse_substring(my_words[i..j]) + # while i < j + # temp = my_words[i] + # my_words[i] = my_words[j] + # my_words[j] = temp + # i += 1 + # j -= 1 + # end i = k + 1 end k += 1 @@ -38,8 +42,26 @@ def reverse_words(my_words) return my_words end +def reverse_substring(string) + first = 0 + last = string.length - 1 + + while first < last + temp = string[first] + string[first] = string[last] + string[last] = temp + first += 1 + last -= 1 + end + return string +end + + + # A method to reverse the words in a sentence, in place. def reverse_sentence(my_sentence) + # string_reverse(my_sentence) + # reverse_words(my_sentence) return reverse_words(string_reverse(my_sentence)) end @@ -69,9 +91,53 @@ 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" + passing_index = 0 + replacing_index = 0 + + + while passing_index < my_string.length + replacing_index += 1 + if my_string[passing_index] == my_string[passing_index + 1] + temp = my_string[passing_index] + n = 1 + passing_index += 1 + # binding.pry + + while temp == my_string[passing_index] + n += 1 + my_string[passing_index] = "^" + passing_index += 1 + end + my_string[replacing_index] = n.to_s + replacing_index += n - 1 + else + passing_index += 1 + end + end + + remove_char(my_string) end +#I looked this method up after struggling on my own for a while and coming up with a method taht had a time complexity of factorial... +def remove_char(my_string) + i = 0 + count = 0 + while i < my_string.length + if my_string[i] != "^" + my_string[count] = my_string[i] + count += 1 + end + i += 1 + end + + i = count + while i < my_string.length + my_string[i] = " " + i += 1 + end +end + + ### ---- END OF METHODS puts "Test 1: reverse a string" my_string = "Lovelace" @@ -117,22 +183,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."