From 8710696cd6c5ca4b0513d3ef414f36bafc5e2422 Mon Sep 17 00:00:00 2001 From: Laura Robertson Date: Sat, 30 Sep 2017 12:39:28 -0700 Subject: [PATCH] All required methods written --- README.md | 12 ++++----- string_manipulation.rb | 56 ++++++++++++++++++++++++++++++++++++++---- 2 files changed, 57 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 05cae71..a29ff88 100644 --- a/README.md +++ b/README.md @@ -4,15 +4,15 @@ Remember that a string is an array of characters. Algorithms that worked on rest ## Exercises Design and implement a method for each of the following. Do not use Ruby provided functionality for `.reverse` and `.reverse!`. You may use `.length`. Share and explain the time and space complexity for each method. -1. Design and implement a method to reverse a string in place. +1. Design and implement a method to reverse a string in place. - e.g. original string = "Lovelace" - reversed string = "ecalevoL" -2. Design and implement a method to reverse each word in a sentence, in place. - - e.g. original = "I can be an   engineer" - - reversed words = "I nac eb na   reenigne" (Note how the count of white spaces gets preserved) +2. Design and implement a method to reverse each word in a sentence, in place. + - e.g. original = "I can be an ; ; ;engineer" + - reversed words = "I nac eb na ; ; ;reenigne" (Note how the count of white spaces gets preserved) 3. Design and implement a method to reverse the words in a sentence, in place. - - e.g. original = "Yoda   is    awesome" - - reversed sentence = "awesome    is   Yoda" (Note how the count of white spaces gets preserved) + - e.g. original = "Yoda ; ; ;is; ; ; ;awesome" + - reversed sentence = "awesome ; ; ; ;is ; ; ;Yoda" (Note how the count of white spaces gets preserved) 4. Design and implement a method to check if the input string is a palindrome. Return true if the string is a palindrome. Return false otherwise. - **Note**: Palindrome is a word, phrase or sentence that reads the same backwards as forwards. e.g. "madam" - [**Optional challenge**] Make the method ignore white spaces. e.g. Make it so that the method will return true for "nurses run" diff --git a/string_manipulation.rb b/string_manipulation.rb index c8b52e1..b82ac6e 100644 --- a/string_manipulation.rb +++ b/string_manipulation.rb @@ -1,22 +1,68 @@ -# A method to reverse a string in place. + # A method to reverse a string in place. def string_reverse(my_string) - puts "NOT IMPLEMENTED" + length = my_string.length + + start = 0 + until start >= length / 2 + last = length - 1 - start + temp = my_string[start] + my_string[start] = my_string[last] + my_string[last] = temp + start += 1 + end + + return my_string + # puts "NOT IMPLEMENTED" end # A method to reverse each word in a sentence, in place. def reverse_words(my_words) - puts "NOT IMPLEMENTED" + start = 0 + last = 0 + + while start < my_words.length - 1 + while my_words[start] == " " + start += 1 + end + + last = start + while my_words[last + 1] != " " && last != my_words.length - 1 + last += 1 + end + + b = last + a = start + + while a < b + temp = my_words[a] + my_words[a] = my_words[b] + my_words[b] = temp + a += 1 + b -= 1 + end + + start = last + 2 + end + # puts "NOT IMPLEMENTED" end # A method to reverse the words in a sentence, in place. def reverse_sentence(my_sentence) - puts "NOT IMPLEMENTED" + string_reverse(my_sentence) + return reverse_words(my_sentence) + # puts "NOT IMPLEMENTED" 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" + position = 0 + while position < my_phrase.length / 2 + if my_phrase[position] != my_phrase[my_phrase.length - 1 - position] + return false + end + position += 1 + end return true end