Skip to content
Open
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
139 changes: 110 additions & 29 deletions string_manipulation.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,110 @@
# 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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you're reversing the string in place, you don't need to return anything. In fact, you can implement this without assigning a new reference, reversed_string to the object that my_string refers to.

end

# A method to reverse each word in a sentence, in place.
def reverse_words(my_words)
puts "NOT IMPLEMENTED"
i = 0
start = 0
segment = ""

#check for space at start
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)
segment = ""
start = i + 1
else
segment << my_words[i]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are creating new memory here by adding each character in every word to segment. So the current space complexity of your method is O(n) if there is only one word in my_words or O(k) where k is the length of the longest word in the input string. Can you author this method without using this additional space and indeed make your space complexity O(1)?

end #if statement
i += 1
end #while statement
my_words[start..(i-1)] = string_reverse(segment)
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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once you update reverse_words to have a space complexity of O(1), the space complexity of this method will also become O(1).

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

until i >= j
if my_phrase[i] == " "

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!
As an added optimization, consider making the conditional statements into loops for skipping white spaces in my_phrase - just to make your method even more flexible.

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
# 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
Expand All @@ -50,6 +130,7 @@ def encode_repeating(my_string)
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}"
Expand All @@ -66,28 +147,28 @@ def encode_repeating(my_string)
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 "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"
# 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."