Just managed to implement working solution. Any feedback would be great. Also have question about… test cases i guess. The question will be under solution.
function palindrome(str) {
/*formatting input
1st step - get rid of non-alphanumeric char-s using regexp
2nd step - avoid case sensitivity-related problems
*/
let nonAlphaNumeric = /(_)*(\W)*/g;
let formattedStr = str.replace(nonAlphaNumeric, '');
formattedStr = formattedStr.toLowerCase();
//Recursion
//base case => empty string or 1-char string obviously palindrome
if (formattedStr == '' || formattedStr.length == 1) {
return true;
}
/*recursive step
assume we have formattedStr => "abba"
assume we've already checked all formattedStr except first and last character:
that means we already know that 'bb' is palindrome
'bb' in general case can be expressed => formattedStr.slice(1,-1)
to check if 'abba' is palindrome we also need to ensure
that 'a'=='a', in general case =>
=>formattedStr[0] == formattedStr[formattedStr.length - 1]
*/
else {
return palindrome(formattedStr.slice(1,-1)) &&
formattedStr[0] == formattedStr[formattedStr.length - 1];
}
}
So my question is about specific cases like this(all input consists of special symbols):
There is no such cases in the challenge tests.
So idea of the challenge is that we ignore any special symbols no matter what?
Or maybe function should have some specific checks for such instances and return false?
There’s couple test cases including non-alphanumeric characters. If those characters would be taken under consideration, these test cases would not pass.
Generally intent here is good, there’s though one detail here. Consider what happens when string has different first and last letter, but otherwise the corresponding letters taken from beginning and end are matching. You might consider whole function and sort of walk through the execution, for some case matching that requirement.
Answer will be correct, yes. I’m trying to hint something else. I suggested using longer string, as the issue kind of exposes itself more in such case. Consider longer string, maybe even ridiculously long. First and last letter are different, the letters in the middle are the same. What function needs to do to come to conclusion that such string isn’t a palindrome?
Well, for now I’m not sure what is the issue. You refer to the bigger size of input, so my suspicion is that it could be optimized, but I need more skills/knowledge to do it. Or maybe it’s about big O notation and efficiency? I’m eager to look into it, if you can provide some useful links that would be cool.
You already have the knowledge to change that, I can assure you. Maybe try to think how you’d by hand check if some long string is palindrome. How that would be different from the function? What in function would need to be modified to make it more similar, specifically in the code pointed before?
So if I will try to generalize this case and will say:
'In situations:
<expression1>&&<expression2>
expression1 should be more simple check’, I will be correct? Can I use such concept as a rule? Or there are some hidden issues with this?
Also for situations like
<expression1>||<expression2>
order is not really important??? Program will need to check both expressions anyway, as I understand.
And finally, some time ago I was told that mathematical logic is useful for coding, and now such advice makes more and more sense. What do you think about that?