Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions .github/PULL_REQUEST_TEMPLATE
Original file line number Diff line number Diff line change
Expand Up @@ -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. | |
163 changes: 137 additions & 26 deletions string_manipulation.rb
Original file line number Diff line number Diff line change
@@ -1,32 +1,143 @@
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
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
if my_words[k] == " " || my_words[k] == nil
j = k - 1

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
end
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)
puts "NOT IMPLEMENTED"
# string_reverse(my_sentence)
# reverse_words(my_sentence)
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

# 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"
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"
Expand Down Expand Up @@ -67,27 +178,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."
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."