If you've ever tried to write palindromic code before, you'd know how much brackets tend to get in your way. ()()
is not a palindrome, even though it kinda looks like it should be, while ())(
and ()(
are both palindromic and both very dumb looking. Wouldn't it be convenient if it was the other way around?
A string is conveniently palindromic if it is equal to the string derived when its reverse has all its parentheses (()
), brackets ([]
), and braces ({}
) flipped. No other characters are special and require flipping. (<>
are sometimes paired but often not so they are left out.)
Your task is to write, in your language, a program (taking input on STDIN) or a function (taking a single string argument) which (a) gives a consistent true value* when its argument is conveniently palindromic and a different, consistent false value otherwise, and (b) is itself conveniently palindromic.
For example, the following inputs are conveniently palindromic:
racecar
(a)(bb)(a)
void main(int argc, *char[] argv) {} (vgra []rahc* ,cgra tni)niam diov
And the following are not:
non-palindrome
A nut for a jar of tuna?
(old [style] parens) )snerap ]elyts[ dlo(
ingirumimusnocte)etconsumimurigni
You may not rely on any external state (specific filename, directory structure, other user input, web access, etc) except interpreter/compiler flags.
Also, you may not use "the comment trick" where you comment out or render unused some piece of code by taking advantage of your language's commenting facilities. For instance, all of the following are not allowed, because they contain non-functional parts that can be safely removed or destroyed (at the expense of losing conveniently-palindromic-ness):
{some code} // {edoc emos}
{some code} NB.BN {edoc emos}
"n\" ;{edoc emos} ;"; {some code}; "\n"
Obviously this might not cover every such case, but the spirit of the challenge here is not to use comments and unparsed** code to achieve palindrominess, instead making use of the corrected parens and brackets. I'm looking at you, LISP, Brainfuck.
This is a code-golf, so the shortest code wins, but all lengths of code are welcome.
* By consistent true and false values, I mean that you can return one of a pair of values, such as 1
for true and 0
for false, or False
for true and "no"
for false, so long as these values are different from each other, and do not change from run to run of your program. Use whatever saves you characters.
** Not to be confused with unexecuted: code that is valid and might do weird things but never called is fine.
if(false){some code}
or unused variables? Are they allowed? \$\endgroup\$(eslaf)fi
, you get to useif(false)
. \$\endgroup\$()()
is not a palindrome \$\endgroup\$