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
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
56 changes: 51 additions & 5 deletions string_manipulation.rb
Original file line number Diff line number Diff line change
@@ -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

Expand Down