forked from Ada-C8/StringManipulationInterviewPractice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_manipulation.rb
More file actions
127 lines (112 loc) · 3.63 KB
/
string_manipulation.rb
File metadata and controls
127 lines (112 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
require "pry"
# A method to reverse a string in place.
def string_reverse(my_string, palindrome: false)
l = my_string.length
i = 0
until i >= l
temp = my_string[i]
my_string[i] = my_string[l - 1]
if palindrome == true
if temp != my_string[i]
return false
end
end
my_string[l - 1] = temp
i += 1
l -= 1
end
# return my_string
# ^return here is necessary for reverse_words. I know its not ideal.
end
def reverse_words(my_words)
i = 0
j = 0
k = 0
l = my_words.length
until i > l
if my_words[i] == " " || i==l
k = i
my_words[j...k] = string_reverse(my_words[j...k])
# ^ this is why I need the string_reverse to return itself - how do I get around this?
j = k + 1
end
i += 1
end
# return my_words
end
# A method to reverse the words in a sentence, in place.
def reverse_sentence(my_sentence)
string_reverse(my_sentence)
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)
string_reverse(my_phrase, palindrome: true) != false
end
# A method that updates the string by replacing consecutive repeating characters
# with a number representing the frequency. The replacement is done only if the
# string length will get reduced by the process.
def encode_repeating(my_string)
puts "NOT IMPLEMENTED"
end
### ---- END OF METHODS
# puts "Test 1: reverse a string"
# my_string = "Lovelace"
# puts "Original string: #{my_string}"
# reversed_string = "ecalevoL"
# string_reverse(my_string)
# if my_string == reversed_string
# puts "String reversed correctly. Reversed string: #{reversed_string}"
# else
# puts "BUG! The reversed string should be '#{reversed_string}' and not '#{my_string}'"
# end
puts "Test 2: reversed words"
my_words = "I can be an engineer"
puts "Original: #{my_words}"
reversed_words = "I nac eb na reenigne"
reverse_words(my_words)
if my_words == reversed_words
puts "Words reversed correctly. Reversed words: #{reversed_words}"
else
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}"
reversed_sentence = "awesome is Yoda"
reverse_sentence(sentence)
if sentence == reversed_sentence
puts "Sentence reversed correctly. Reversed sentence: '#{reversed_sentence}'"
else
puts "BUG! The reversed sentence should be '#{reversed_sentence}' and not '#{sentence}'"
end
puts "Test 4: Palindrome check"
phrase = "madam"
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 "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."