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
172 changes: 145 additions & 27 deletions string_manipulation.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,148 @@
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
end

# A method to reverse each word in a sentence, in place.
def reverse_words(my_words)
puts "NOT IMPLEMENTED"
i = 0
sub_start = 0
sub_end = 0
while i < my_words.length
if my_words[i] != " "
until my_words[i] == " " || i == my_words.length
i += 1
end

sub_end = i - 1

while sub_start <= sub_end && i <= my_words.length
temp = my_words[sub_start]
my_words[sub_start] = my_words[sub_end]
my_words[sub_end] = temp
sub_end -= 1
sub_start += 1
end

sub_start = i

else
i += 1
sub_start += 1
end
end
end

# A method to reverse the words in a sentence, in place.
def reverse_sentence(my_sentence)
puts "NOT IMPLEMENTED"
i = 0
j = my_sentence.length - 1

while i < j
temp = my_sentence[i]
my_sentence[i] = my_sentence[j]
my_sentence[j] = temp
i += 1
j -= 1
end

i = 0
sub_start = 0
sub_end = 0

while i < my_sentence.length
if my_sentence[i] == " "
i += 1
sub_start += 1
else
until my_sentence[i] == " " || i == my_sentence.length
i += 1
end

sub_end = i - 1

while sub_start <= sub_end && i <= my_sentence.length
temp = my_sentence[sub_start]
my_sentence[sub_start] = my_sentence[sub_end]
my_sentence[sub_end] = temp
sub_end -= 1
sub_start += 1
end

sub_start = i
end
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"
return true
forwards = ""
backwards = ""

j = 0
while j < my_phrase.length
unless my_phrase[j] == " "
forwards << my_phrase[j]
end
j += 1
end

i = my_phrase.length - 1

while i >= 0
unless my_phrase[i] == " "
backwards << my_phrase[i]
end
i -= 1
end

if forwards == backwards
return true
else
return false
end
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"
first = my_string[0]
i = 0
count = 0
while i < my_string.length
until my_string[i] != first || i == my_string.length
i += 1
count += 1
end

if count > 2
my_string[i - count] = first
my_string[i - count + 1] = count.to_s

(count - 2).times do
my_string[i - 1] = ""
i -= 1
end
end

first = my_string[i]
count = 0
end
end

### ---- END OF METHODS
Expand Down Expand Up @@ -67,27 +185,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."