by YeGoblynQueenne · 10y ago · 0 comments
Woohooa, cowboy. The language of palindromes is a classic example of a context free language that is not possible to describe with a regular grammar, such as accepted by a regular automaton... in other words a regex.
In Recruiter-Parseable English (RPE): you can't decide whether a string is a palindrome using a regex. It's impossible. Like, maths-impossible.
You can use regular expressions to compare strings while you decide whether a string is a palindrome but you can't do the whole thing in one regex.
https://gist.github.com/anonymous/e8553707cd6162040e7d12b2a1...
stringToTest = stringToTest.toLowerCase().replace(/[^a-z0–9]/ig, '');
But the example solution is so bad anyway.
is_a_palindrome(S) {
assert is_a_string(S)
if reverse(S) == S
return true
return false
}
A string is a palindrome if it's equal to itself reversed. The example "solution" in the article is dreadfully overengineered to death and back. Noone interviewing should be happy to see such a solution and noone being interviewed should expect to be required to give such a solution, to such a trivial problem.Unless of course the recruiter is trying to catch you pants-down, but then the parent article wouldn't post that overengineered "solution" as a gold-standard, like they did.