104
19
There have been a couple of previous attempts to ask this question, but neither conforms to modern standards on this site. Per discussion on Meta, I'm reposting it in a way that allows for fair competition under our modern rulesets.
Background
A palindrome is a string that "reads the same forwards and backwards", i.e. the reverse of the string is the same as the string itself. We're not talking about "convenient palindromes" here, but a strict character-by-character reversal; for example, ()()
is not a palindrome, but ())(
is.
The task
Write a program or function that takes a string S (or the appropriate equivalent in your language) as input, and has one output Q (of a type of your choice). You can use any reasonable means to take the input and provide the output.
- When the input S is a palindrome, the output Q should have a value A (that is the same for any palindromic S).
- When the input S is not a palindrome, the output Q should have a value B (that is the same for any non-palindromic S).
- A and B must be distinct from each other.
Or in other words: map all palindromes to one value, and all non-palindromes to another.
Additionally, the program or function you write must be a palindrome itself (i.e. its source code must be palindromic), making this a restricted-source challenge.
Clarifications
- Although
true
andfalse
are obvious choices for A and B, you can use any two distinct values for your "is a palindrome" and "isn't a palindrome" outputs, which need not be booleans. - We're defining string reversal at the character level here;
éé
is palindromic regardless of whether the program is encoded in UTF-8 or Latin-1, even though it's not a palindromic sequence of octets after UTF-8 encoding. - However, even if your program contains non-ASCII characters, it only needs to work for ASCII input. Specifically, the input S will only contain printable ASCII characters (including space, but not including newline). Among other things, this means that if you treat the input as a sequence of bytes rather than a sequence of characters, your program will still likely comply with the specification (unless your language's I/O encoding is very weird). As such, the definition of a palindrome in the previous bullet only really matters when checking that the program has a correct form.
- Hiding half the program in a comment or string literal, while being uncreative, is legal; you're being scored on length, not creativity, so feel free to use "boring" methods to ensure your program is a palindrome. Of course, because you're being scored on length, parts of your program that don't do anything are going to worsen your score, so being able to use both halves of your program is likely going to be helpful if you can manage it.
- Because the victory criterion is measured in bytes, you'll need to specify the encoding in which your program is written to be able to score it (although in many cases it will be obvious which encoding you're using).
Victory criterion
Even though the program needs to be a palindrome at the character level, we're using bytes to see who wins. Specifically, the shorter your program is, measured in bytes, the better; this is a code-golf challenge. In order to allow submissions (especially submissions in the same language) to be compared, place a byte count for your program in the header of your submission (plus a character count, if it differs from the number of bytes).
12Would someone please explain why would ()() not be a palindrome?? – Emilio M Bumachar – 2017-02-20T06:38:31.583
60@EmilioMBumachar Try replacing
(
witha
and)
withb
. Isabab
a palindrome? No, it would have to beabba
. Then()()
isn't a palindrome either; it would have to be())(
. – DLosc – 2017-02-20T06:40:29.0877Those solutions entirely using comments to make the program palindromic looks like a loophole to me :( – kennytm – 2017-02-20T08:26:06.640
2@kennytm The OP himself allows it. – seshoumara – 2017-02-20T08:36:18.483
Can I give no output if S is not a palindrome and print 1 if it is? – seshoumara – 2017-02-20T08:55:13.847
16@kennytm Disallowing them would be worse, because there's no satisfactory way to do that objectively in a language-agnostic way. (What's a comment? What about putting the unused half in a string literal that is discarded? What about 2D languages where you can have perfectly executable code that is simply never reached?) – Martin Ender – 2017-02-20T09:08:09.117
I think another interesting result of this could be the longest program in which every bit of code is executed. I.E., there are no comments or sections of code that are never reached just to make it a palindrome. – Engineer Toast – 2017-02-20T15:58:59.863
@EngineerToast: We've had a few challenges along those lines. The solution nearly always ends up to be putting most of the program in a string literal, and then using some sort of checksum to ensure that it actually has an effect on the program, which probably isn't what you were expecting. – None – 2017-02-20T23:34:00.890
1I had to join this stack just to leave a comment and up-vote here. It boggles the mind how you guys come up with these challenges that look complex yet get solved in 3 bytes in a bunch languages, even regular ones. – KalleMP – 2017-02-21T10:13:36.150
10
()() is not a palindrome, but ())( is.
Congratulations, you made it onto reddit! – numbermaniac – 2017-02-25T06:31:39.083This one popped back up to the top of the stack and I have to ask, is
éé
a palindrome? (Fair warning, it is not strictly equal toéé
). Reversed by unicode character endpoints, it would béée
. – Draco18s no longer trusts SE – 2019-11-29T15:32:01.597