RSPEC - Exercice 2 "palindrome_strings"

Hi
My problem is the following : Rspec returns an error concerning the “should call your previous subtrings method” condition :

But my outputs are correct and i’ve used the subtrings method once :

So i’m confused…

I did not take a close look at the issue, but I suspect the RSPEC file may be looking for the Monkey Patching / class extention version of the method and subsequent call ( arr#substrings ).

1 Like

I just solved this earlier today. Looks nothing like your answer or the answer in the solution, but it works in a way I understand and that’s the most important thing being a successful programmer I have heard. Your code sorta confuses me, but I think what you might be doing wrong is the same issue I had. You are case checking the entire string if it is a palindrome right from the start. What the problem is asking for in the spec file is to first split the string into all parts > 1 char and return those which ARE palindromes.
[it “should return an array containing all substrings that are palindromes and longer than 1 character” do
expect(palindrome_substrings(“abracadabra”)).to match_array [“aca”, “ada”]
]

—My solution —
def palindrome?(str)
arr = str.split("")
reverse = []
arr.each {|c| reverse.unshift© }
reversed = reverse.join("")
if str == reversed
return true
end
return false
end

def substrings(string)
s = string.split("") #[“j”, “u”, “m”, “p”]
arr = []
arr1 = []
x = 0
while x < s.length
s.each_with_index do |char, i|
arr << s[x…i]
end
x +=1
end
arr.each_with_index do |char, i|
if char != []
arr1 << char
end
end
return arr1.map {|ele| ele.join("")}
end

def palindrome_substrings(string)
ar = []
arr = substrings(string)
arr.each do |a|
if a.length > 1 && palindrome?(a) == true
ar << a
end
end
return ar
end

1 Like

@poisonouschimp Yeah i think you’re right about my early palindrome checking