I'm a palindrome. Are you?

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 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 challenge.

Clarifications

  • Although true and false 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 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).

user62131

Posted 2017-02-20T01:34:20.470

Reputation:

12Would someone please explain why would ()() not be a palindrome?? – Emilio M Bumachar – 2017-02-20T06:38:31.583

60@EmilioMBumachar Try replacing ( with a and ) with b. Is abab a palindrome? No, it would have to be abba. Then ()() isn't a palindrome either; it would have to be ())(. – DLosc – 2017-02-20T06:40:29.087

7Those 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.083

This 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 be ́ée. – Draco18s no longer trusts SE – 2019-11-29T15:32:01.597

Answers

138

Brachylog (2), 3 bytes in Brachylog's codepage

I↔I

Try it online!

This is a full program that takes input via standard input (using Brachylog's syntax for constants, i.e. strings are enclosed in double quotes), and outputs via standard output. The outputs are true. for a palindromic input, and false. for a non-palindromic input.

Not only is this program palindromic, it also has left/right (and probably in some fonts up/down) mirror symmetry.

Explanation

In Brachylog, capital letters mark points in the program which have identical values; this is used almost like an electrical circuit to carry information from one part of the program to another. One consequence of this is that if you enclose a command between an identical pair of capital letters, you're effectively asserting that the command's input and output are the same. Brachylog implicitly takes input, so in this case we're also asserting that the input to the command is the same as the input to the program. In this program, we're using the command , which reverses things (in this case, strings); so the program effectively asserts that the input is the same forwards and backwards.

A full program (as opposed to a function) in Brachylog returns a boolean, false. if there's no way to make all the assertions in the program correct at once, or true. if the assertions in the program are all compatible with each other. We only have one assertion here – that reversing the input does not change it – so the program acts as a palindrome checker.

user62131

Posted 2017-02-20T01:34:20.470

Reputation:

49And 180 degree rotational symmetry, It's beautiful. – ATaco – 2017-02-20T02:07:03.130

7... and symmetry along vertical and horizontal axes :-) – Luis Mendo – 2017-02-20T09:56:35.417

2More like 5 bytes in UTF-8, isn't it? – SteakOverflow – 2017-02-20T16:41:35.223

13

@SteakOverflow Brachylog uses a custom code-page, so those characters are not encoded in UTF-8

– James – 2017-02-20T21:46:08.637

4I joined this community just to up vote this program. Wow. – Bill Michell – 2017-02-21T12:25:33.753

4@ATaco The combination of left/right and up/down symmetries imply 180 degree rotational symmetry. ;) – Eric Duminil – 2017-02-23T21:52:50.733

55

Pyth, 3 bytes

_I_

Returns True or False.

Try it online!

How it works

  _  Reverse the input.
_I   Invariant-reverse; test if the reversed input is equal to its reverse.

Dennis

Posted 2017-02-20T01:34:20.470

Reputation: 196 637

1Why do you need the final _? – busukxuan – 2017-02-20T07:30:37.167

34@busukxuan From the question, "Additionally, the program or function you write must be a palindrome itself" – isaacg – 2017-02-20T07:41:18.680

1Why so many upvotes...This answer doesn't seem that hard to come up with...? – ghosts_in_the_code – 2017-02-26T16:08:32.597

@ghosts_in_the_code The vast majority of answers to this challenge weren't difficult to come up with... – Dennis – 2017-02-26T16:12:17.820

2I guess so. Still it seems kind of unfair. On some questions, one must put a lot of hard work to answer, and others are much easier. Still the payout is the same. Btw I've also upvoted :P – ghosts_in_the_code – 2017-02-26T16:13:57.133

4@ghosts_in_the_code Only one of my answers with 100+ was actually challenging to write, yet there are answers I spent days on that only got a handful of upvotes. In the end, it all evens out... – Dennis – 2017-02-26T16:20:09.103

47

Python, 39 bytes

lambda s:s[::-1]==s#s==]1-::[s:s adbmal

Try it online!

Boring, but if there is shorter in Python it will be impressive.

Jonathan Allan

Posted 2017-02-20T01:34:20.470

Reputation: 67 804

Wow, thos (, ) were some good (and confusing) inputs :) – ABcDexter – 2017-02-21T05:39:06.850

34

Jelly, 5 bytes

ḂŒ
ŒḂ

Returns 1 or 0. The first line is an unexecuted helper link, the second line calls the palindrome test.

Try it online!

Dennis

Posted 2017-02-20T01:34:20.470

Reputation: 196 637

wow, recent addition. – Jonathan Allan – 2017-02-20T02:21:07.757

6Yep, only 18 hours old. – Dennis – 2017-02-20T02:22:02.437

you didn't specify the encoding. I'm guessing UTF-8? – Brian Minton – 2017-02-24T17:16:34.023

1

@BrianMinton No, this would be 11 bytes in UTF-8. Jelly uses this code page.

– Dennis – 2017-02-24T17:28:35.240

@Dennis, thanks for the info. – Brian Minton – 2017-02-24T17:34:03.643

24

Jelly, 5 bytes

⁼ṚaṚ⁼

Try it online!

Equals reverse and reverse equals.

Or the more efficient yet less aesthetically pleasing:

⁼Ṛ
Ṛ⁼

or

Ṛ⁼
⁼Ṛ

Jonathan Allan

Posted 2017-02-20T01:34:20.470

Reputation: 67 804

24

Mathematica, 23 bytes

QemordnilaP;PalindromeQ

Not very interesting, but for the sake of completeness...

The above is a CompoundExpression which evaluates to PalindromeQ, a built-in that solves the challenge. QemordnilaP is simply an undefined identifier, which is ignored because of the ;.

Martin Ender

Posted 2017-02-20T01:34:20.470

Reputation: 184 808

22

Haskell, 87 85 44 34 bytes

p=(==)<*>reverse--esrever>*<)==(=p

Explanation: ((->) a) is an instance of Applicative (thanks @faubiguy), with <*> defined as

(<*>) f g x = f x (g x)

So by substituting in the arguments one can see why this works.

Program man

Posted 2017-02-20T01:34:20.470

Reputation: 531

1Can you explain the code? – bli – 2017-02-20T06:48:05.437

1@bli everything after the -- is a comment. – theonlygusti – 2017-02-20T09:05:55.947

3@theonlygusti Haskell is sufficiently alien that that only half helps. – Yakk – 2017-02-21T18:41:39.427

@Yakk It's some sort of combination of the (==), reverse, and id functions (id is the identity function). – tbodt – 2017-02-21T22:08:56.373

You can save 10 bytes by using <*> instead of <$> and removing the <*>id – faubi – 2017-02-22T02:36:45.037

Nice thinking! Applicative is still weird to me... – Program man – 2017-02-22T03:11:17.667

22

05AB1E, 3 bytes

Code:

ÂQÂ

Explanation:

     # Bifurcate (duplicate and reverse the duplicate) implicit input
 Q    # Check if equal
  Â   # Bifurcate the result

Uses the CP-1252 encoding. Try it online!

Adnan

Posted 2017-02-20T01:34:20.470

Reputation: 41 965

Why not just ÂQ – Neil A. – 2017-05-22T00:20:45.123

2@NeilA. The code itself needs to be a palindrome as well. – Adnan – 2017-05-22T04:22:21.240

18

PHP, 55 bytes

<?=strrev($s=$_GET[s])==$s;#;s$==)]s[TEG_$=s$(verrts=?<

Plus, the name of the language is a palindrome so... bonus points!

Kodos Johnson

Posted 2017-02-20T01:34:20.470

Reputation: 776

Sneaky solution. – Martijn – 2017-02-21T15:02:15.643

17

MATL, 7 bytes

tPX=XPt

Try it online!

Returns [1; 1] for palindromic input and [0; 0] otherwise.

t       % duplicate the input
P       % reverse the second string
X=      % check the two strings are exactly equal (returns 0 or 1)
XP      % flip array (does nothing)
t       % duplicate the answer, giving either [1;1] or [0;0]
        % (implicit) convert to string and display

B. Mehta

Posted 2017-02-20T01:34:20.470

Reputation: 763

16

Pip, 12 11 bytes

Now comment-free!

x:RVaQaVR:x

Takes input as a command-line argument; outputs 1 for palindrome, 0 for non-palindrome. Try it online!

The core of what we want to do is RVaQa: reverse(a) string-equals a. The code x:RVaQa calculates this result and assigns it to x. Then VR:x assigns the value of x to the variable VR. Since this assignment is the last statement in the program, its value is also autoprinted. Voila!

For a previous interesting version using some undefined behavior, see the revision history.

DLosc

Posted 2017-02-20T01:34:20.470

Reputation: 21 213

13

Perl 6, 25 bytes/chars utf8

{.flip eq$_}#}_$qe pilf.{

Try it

Brad Gilbert b2gills

Posted 2017-02-20T01:34:20.470

Reputation: 12 713

10

R, 111 103 bytes

all((s<-el(strsplit(scan(,"",,,"\n"),"")))==rev(s))#))s(ver==)))"",)"n\",,,"",(nacs(tilpsrts(le-<s((lla

Not the most original answer. # is the comment character in R

Ungolfed:

all((s<-el(strsplit(scan(,"",,,"\n"),"")))==rev(s))
#
))s(ver==)))"",)"n\",,,"",(nacs(tilpsrts(le-<s((lla

The character string from scan is converted into raw bytes thanks to the charToRaw function. These raw bytes are compared one-by-one to their counterparts from the rev() function, which reverses the order of its argument. The output of this part is a vector of TRUE and/or FALSE.
The all function then outputs TRUE if all those elements are TRUE

Here, "\n" in the scan function is necessary for inputs with more than one word.

Previous answer (byte-wise), 81 bytes

function(s)all((s=charToRaw(s))==rev(s))#))s(ver==))s(waRoTr‌​ahc=s((lla)s(noitcnu‌​f

with - 24 bytes thanks to @rturnbull.

Frédéric

Posted 2017-02-20T01:34:20.470

Reputation: 2 059

You can save a good few bytes by doing the charToRaw conversion before assignment to s, and changing how you set the sep argument to scan: all((s<-charToRaw(scan(,"",,,"\n")))==rev(s))#))s(ver==)))"n\",,,"",(nacs(waRoTrahc-<s((lla – rturnbull – 2017-02-21T09:41:15.347

(Also, this approach doesn't work for e.g. input éé under a UTF-8 encoding, but I don't think that breaks the rules of the challenge.) – rturnbull – 2017-02-21T09:45:46.607

@rturnbull : thanks for the inputs ! I indeed tested éé with a latin1 encoding. – Frédéric – 2017-02-21T10:32:47.487

Since the test must be done character-wise, I think the current programm breaks the rules. – Frédéric – 2017-02-21T10:42:54.130

I'm not so sure the previous version does break the rules. OP states: "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)." – rturnbull – 2017-02-21T11:39:36.117

But if you insist on the new version, you can replace s<-unlist( with s=el( to save a few bytes. – rturnbull – 2017-02-21T11:42:44.057

A further improvement on the byte-comparison method: an anonymous function for 81 bytes function(s)all((s=charToRaw(s))==rev(s))#))s(ver==))s(waRoTrahc=s((lla)s(noitcnuf – rturnbull – 2017-02-21T11:47:53.047

@rturnbull : It's the "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" part that made me doubt. Nice function too ! – Frédéric – 2017-02-21T12:12:23.180

@count : It does work as is, but since nothing is said about lower/uppercase, you could drop this part. strsplit(x)[[1]] can be replace by el(strsplit(x)). You end up with a 56 bytes function(x)paste(rev(el(strsplit(x,""))),collapse="")==x anonymous function. In the palindromic-ing process, you gain 57 bytes. Nice inputs though ! – Frédéric – 2017-02-21T15:01:58.853

This isn't a palindrome, the parenthesis are wrong. – Russell Greene – 2017-02-24T00:24:59.223

utf8ToInt is shorter than strsplit. It'll work for all ASCII: try it online – Giuseppe – 2018-04-05T13:28:10.457

9

RProgN, 11 bytes

~]S.E E.S]~

The first half of this does all the heavy lifting, and by a convenience of RProgN, the second half is a No-op.

~]S.E E.S]~
~           # Treat the word as a Zero Space Segment
 ]          # Duplicate the top of the stack
  S.        # Reverse the top of the stack
    E       # Compare if these values are equal
      E.S]~ # A no-op, because the ~ is at the end of the word, not the start.

Try it online!

ATaco

Posted 2017-02-20T01:34:20.470

Reputation: 7 898

9

GNU sed, 64 59 + 1(r flag) = 60 bytes UTF-8

Took me a while to come up with a sed answer that is not using a comment section to make the code a palindrome. Instead, I use the c command that would print the first half of the code in reverse order, only I make sure this instruction is not reached.

:;s:^(.)(.*)\1$:\2:;t;/../c1
d
1c/../;t;:2\:$1\)*.().(^:s;:

The script prints 1 if the input string is not a palindrome (think of it as giving an error). If the string is a palindrome, then no output is given (think of it as exiting successfully).

Run examples: or Try it online!

me@LCARS:/PPCG$ sed -rf palindrome_source.sed <<< "level"
me@LCARS:/PPCG$ sed -rf palindrome_source.sed <<< "game"
1

Explanation:

:                              # start loop
s:^(.)(.*)\1$:\2:              # delete first and last char, if they are the same
t                              # repeat if 's' was successful
/../c1                         # if at least 2 chars are left, print 1. 'c' reads
                               #till EOL, so next command must be on a new line.
d                              # delete pattern space. This line must be a
                               #palindrome itself, and must end the script.
1c/../;t;:2\:$1\)*.().(^:s;:   # (skipped) print first half of code in reverse
                               #order. Everything after 'c' is treated as string.

seshoumara

Posted 2017-02-20T01:34:20.470

Reputation: 2 878

1

TIO has support for sed now. -r doesn't work, but you can just wrap the whole thing in BASH. Try it Online!

– Riley – 2017-02-20T15:56:49.963

@Riley Nice usage of header and footer on TIO, thanks. The previous workaround was to move the code to the argument list with -e, but your way is much nicer. I was waiting for that to be fixed, but this way I don't need to. – seshoumara – 2017-02-20T16:17:53.757

8

Retina, 53 bytes

Byte count assumes ISO 8859-1 encoding.

$
¶$`
O$^`\G.
»
D`
M$`^.+$
$+.^`$M
`D
»
.G\`^$O
`$¶
$

Try it online!

I'm pretty sure this isn't optimal yet (the » line seems particularly wasteful, and I have a 45-byte solution that is palindromic except for one character), but I guess it's a start.

Martin Ender

Posted 2017-02-20T01:34:20.470

Reputation: 184 808

8

Alice, 19 bytes

/@.nzRoi\
\ioRzn.@/

Try it online!

Prints Jabberwocky for palindromes and nothing for non-palindromes. Works for arbitrary UTF-8 input.

Explanation

Since this is a string processing task, Alice will have to operate in Ordinal mode to solve it. That in turn means that the instruction pointer has to move diagonally, and therefore we need at least two lines so that the IP can bounce up and down. The linefeed in such a program makes for a good position to place the middle character of the palindrome. That means the second line needs to be the reverse of the first. But since we're only executing every other character on each line, if we make sure that the line-length is odd, the reverse of the code will neatly fit into its own gaps. The only character that isn't used at all is the backslash, but since it was arbitrary I chose it to make the program look nice and symmetric.

So anyway, the actual relevant code is this:

/ . z o
 i R n @

Which is executed in a zigzag from left to right.

/   Reflect the IP southeast, enter Ordinal mode.
i   Read all input as a single string.
.   Duplicate the input.
R   Reverse the copy.
z   Pop the reverse Y and the original X. If X contains Y, drop everything
    up to its first occurrence. Since we know that X and Y are the same length,
    Y can only be contained in X if X=Y, which means that X is a palindrome.
    So this will result in an empty string for palindromes and in the non-empty
    input for non-palindromes.
n   Logical NOT. Replaces non-empty strings with "", and empty strings with
    "Jabberwocky", the "default" truthy string.
o   Output the result.
@   Terminate the program.

Martin Ender

Posted 2017-02-20T01:34:20.470

Reputation: 184 808

7

Haskell, 34 bytes

f=(==)=<<reverse--esrever<<=)==(=f

Try it online! Call with f "some string", returns True or False.

The =<< operator on functions works like f=<<g = \s -> f (g s) s, so the code is equivalent to f s=s==reverse s, which, as I just noticed, would result in the same byte count.


Version without comment: (49 bytes)

e x y=x/=y
p=e=<<reverse
esrever<<=e=p
y=/x=y x e

Try it online!

Call with p "some string". This outputs False if the given string is a palindrome, and True if it's not a palindrome.

Explanation:

I found this comment free palindrome by starting with the comment version and replacing the the comment with a new line:

p=(==)=<<reverse
esrever<<=)==(=p

The second line fails because the parenthesis do not match, so we need to get rid of them. If we had a function e which checks for equality, then

p=e=<<reverse
esrever<<=e=p

will both compile with the second line defining an infix-operator <<= which takes two arguments esrever and e and returns the function p.

To define e as the equality function one would normally write e=(==), but )==(=e will again not compile. Instead we could explicitly take two arguments and pass them to ==: e x y=x==y. Now the reversed code y==x=y x e compiles but redefines the == operator, which causes the definition e x y=x==y to fail. However if we switch to the inequality operator /=, the reversed definition becomes y=/x=y x e and defines a =/ operator which does not interferes with the original /= operator.

Laikoni

Posted 2017-02-20T01:34:20.470

Reputation: 23 676

6

OIL, 178 bytes

Reads an input, explodes it, slowly adds its length (through incrementing and decrementing) to the address to know to the address after the string, jumps to a different part of code (in the middle), reverses the band direction, implodes the string again, and checks whether it's the same as the original string. TL;DR: It's a pain, as usual.

Outputs 40 if the string isn't a palindrome, 0 if it is.

5
0
12
0
40
1
40
2
1
40
34
10
2
3
22
16
9
2
8
35
6
11
6
37

3
4
4
27
26
0
1
10
1

40
13
2
31
04

1
01
1
0
62
72
4
4
3

73
6
11
6
53
8
2
9
61
22
3
2
01
43
04
1
2
04
1
04
0
21
0
5

L3viathan

Posted 2017-02-20T01:34:20.470

Reputation: 3 151

2Neat language! :) – DLosc – 2017-02-21T03:49:27.633

5

Javascript, 64 bytes

f=s=>s==[...s].reverse().join``//``nioj.)(esrever.]s...[==s>=s=f

Call function f with string

f("abba") // returns true
f("abab") // returns false

Prasanth Bendra

Posted 2017-02-20T01:34:20.470

Reputation: 151

Your source code is not a palindrome! – seshoumara – 2017-02-20T12:39:42.463

@seshoumara Updated the code – Prasanth Bendra – 2017-02-20T12:53:39.880

Now it's fine. Maybe mention the return value if the string is not a palindrome, just for completion sake. – seshoumara – 2017-02-20T13:12:53.080

@apsillers thank you I edited the answer. – Prasanth Bendra – 2017-02-23T09:32:13.257

There is no function f, your code doesn't assign your arrow function to a variable so it can't be called – spex – 2017-02-26T21:24:36.280

@spex anonymous functions are fine to be used – Felipe Nardi Batista – 2017-03-16T13:18:05.970

@FelipeNardiBatista My comment wasn't about anonymous functions, it's that he was saying to call the function with f when at the time it didn't exist. The answer has since been updated and there is now a function f and my comment is now outdated. – spex – 2017-03-16T23:39:03.420

@spex but that made him lose 4 bytes of his answer... I read the edit logs before replying to you, it's ok to do that – Felipe Nardi Batista – 2017-03-17T10:22:07.653

@FelipeNardiBatista Like I said, I'm not against him using an anonymous function. I was against him saying "Call function f with string" when that was impossible. If he had simply changed the usage instructions that would have been a good fix. – spex – 2017-03-17T19:04:06.030

5

Japt, 7 2 bytes

êê

Run it

Old solution:

U¥UwU¥U

Try it online!

Explanation

U¥UwU¥U
U¥        U is the input, ¥ is a shortcut for == 
  Uw      w is a reverse function.
    U¥U   This calculates U == U (always true), but the result is ignored
          because w does not look at its arguments.

Japt doesn't escape functions unless a closing parenthesis (or space) is reached.

This can be re-written: U¥Uw(U¥U)U¥UwU==Uw. In Japt, the parenthesis left out at the begining and end of a function is auto-inserted.

Oliver

Posted 2017-02-20T01:34:20.470

Reputation: 7 160

It all makes sense, except if w is a function that takes no arguments, how does it apply to U? Is it something like U.reverse()? – DLosc – 2017-02-21T05:32:28.573

@DLosc Correct. It reverses U in the same way as U.reverse(). – Oliver – 2017-02-21T05:45:54.153

4

Bash + Unix utilities, 49 bytes

[ "$1" = "`rev<<<$1`" ] # ] "`1$<<<ver`" = "1$" [

Input is passed as an argument.

Output is returned in the result code -- 0 for a palindrome, 1 for a non-palindrome.

Maybe someone can do better and not just rely on a comment to make the code itself palindromic.

Try it online!

Mitchell Spector

Posted 2017-02-20T01:34:20.470

Reputation: 3 392

[[ $1 = \rev<<<$1` ]]is shorter. (Bash[[` syntax, no quote needed) – Arthur2e5 – 2017-02-24T02:03:58.293

2@Arthur2e5 I tried out your suggestion, but I think the quotes around rev<<<$1 are needed even in the [[...]] solution. Test it with input string '[$]]$[' (which is a palindrome). With those quotes added in to make it work, your solution is the same length as my solution. – Mitchell Spector – 2017-02-24T02:57:12.507

Great catch! I forgot that the RHS of == in [[ would be interpreted as a case-like pattern. – Arthur2e5 – 2017-02-24T04:06:37.877

@Arthur2e5 I still think there's probably some clever way to make this shorter. – Mitchell Spector – 2017-02-24T05:03:15.597

Will this still work if there are newlines in the input? I think you need rev|tac instead of just rev. – b_jonas – 2017-06-13T20:00:47.957

@b_jonas The requirements specifically say that the input won't contain newlines. In the 3rd bullet under Clarifications: "Specifically, the input S will only contain printable ASCII characters (including space, but not including newline)." – Mitchell Spector – 2017-06-14T01:41:39.867

Ah, thank you. I indeed missed that part of the spec. – b_jonas – 2017-06-14T11:30:08.807

4

Java 8, 92 90 bytes

This is a comment version. If a string contains its reverse, then it is a palindrome (true) otherwise it is not (false).

s->s.contains(new StringBuffer(s).reverse())//))(esrever.)s(reffuBgnirtS wen(sniatnoc.s>-s

Try it online!

Update

  • -2 [18-04-05] Switched to contains. Thanks to @Kevin Cruijssen!
  • -2 [17-02-20] Removed ;'s
  • -16 [17-02-22] Auto convert

NonlinearFruit

Posted 2017-02-20T01:34:20.470

Reputation: 5 334

This code is not a lambda expression. – Jakob – 2017-08-30T00:21:35.820

@Jakob I thought it was. If you were to use the lambda, you would probably want a leading and trailing newline. (I added a tio link) – NonlinearFruit – 2017-09-05T21:10:45.153

Yeah, my complaint was that the line comment makes the submission more than just a lambda expression, and thus not valid as a lambda solution. Don't worry about it for now; I'll probably eventually make a meta post to gather consensus. – Jakob – 2017-09-06T01:11:17.523

@Jakob Lambda solutions can sometimes have extraneous code, which is why I think it is valid. But if you aren't sold, a meta post wouldn't hurt.

– NonlinearFruit – 2017-09-06T01:46:23.933

Well, the post you reference addresses multiple lambda expressions in a single solution, not code outside the expressions themselves (the author's intent in the last part is just to make function g available to x, not to allow extra code). At least that's the way I see it. – Jakob – 2017-09-06T02:41:10.877

1

I know it's been a while, but you can golf 2 bytes by changing it to s->s.contains(new StringBuffer(s).reverse())//))(esrever.)s(reffuBgnirtS wen(sniatnoc.s>-s. Try it online 90 bytes.

– Kevin Cruijssen – 2018-04-03T08:14:22.797

4

Java - 171 169 160 bytes

int q(String s){return s.equals(new StringBuffer(s).reverse().toString())?1:2;}//};2:1?))(gnirtSot.)(esrever.)s(reffuBgnirtS wen(slauqe.s nruter{)s gnirtS(q tni

The comment at the end is to make it a palindrome. Returns P(alindrome) when the input is palindrome and N(ot) when not.

Ungolfed version:

int q(String s) {
    return s.equals(new StringBuffer(s).reverse().toString()) ? 'P' : 'N';
}//};'N':'P'?))(gnirtSot.)(esrever.)s(reffuBgnirtS wen(slauqe.s nruter{)s gnirtS(q tni

2 bytes saved thanks to @DLosc

Thanks to @Olivier Grégoire for pointing out the incorrect amount of bytes! Fixed now

cookie

Posted 2017-02-20T01:34:20.470

Reputation: 271

I believe you can save some bytes by returning ints instead of chars. – DLosc – 2017-02-21T03:53:02.970

I don't know how do check your byte count, but you have 160 bytes, not 161. – Olivier Grégoire – 2017-02-23T08:37:35.717

You can save 2 bytes by returning 80 for 'P' and 78 for 'N' or use different chars to save even more bytes. – Selim – 2017-02-23T16:57:42.647

1You can save even more bytes by doing new StringBuffer(s).reverse()+"" instead of new StringBuffer(s).reverse().toString() – Selim – 2017-02-23T17:00:52.980

1any reason you're returning an int instead of bool? – CodesInChaos – 2017-02-23T18:02:23.520

Definitely good to have Java answers, and this is in the same spirit as many other answers here, but I can't help but question whether this is valid as a method solution since the comment appears outside the method definition. – Jakob – 2017-08-30T00:19:26.977

3

Actually, 5 bytes

;R=R;

Try it online!

The truthy output is [1]\n[1], and the falsey output is []\n[] (in both outputs, \n represents a literal newline).

Explanation:

;R=R;
;R=    duplicate input, reverse one copy, test equality (the main palindrome-testing part)
   R   range(1, x+1) - if palindrome, this pushes [1], else it pushes []
    ;  duplicate

Mego

Posted 2017-02-20T01:34:20.470

Reputation: 32 998

Why don't you just do this?

– Leaky Nun – 2017-04-24T11:18:16.107

1@LeakyNun it has to be a palindrome – caird coinheringaahing – 2017-04-24T20:23:13.313

3

C++, 154 Bytes

int m(){std::string g,p="";g=p;std::reverse(p.begin(),p.end());return g==p;}//};p==g nruter;))(dne.p,)(nigeb.p(esrever::dts;p=g;""=p,g gnirts::dts{)(m tni

I have to say, the reverse statement was costly, but I can't imagine much I can do to change that. Being able to cut out the std:: symbols would save me around 10 characters, but "using namespace std;" is quite a few more.

I suppose C++ wasn't really meant for brevity.

Monkah VII

Posted 2017-02-20T01:34:20.470

Reputation: 191

3

Prolog, 44 bytes

p-->[]|[_]|[E],p,[E].%.]E[,p,]E[|]_[|][>--p

This uses definite clause grammars. It is actually a full context free grammar:

p -->
      []            % the empty string
   |                % or
      [_]           % a one character string
   |                % or
      [E],          % one character, followed by
      p,            % a palindrome, followed by
      [E].          % that same character

Usage:

?- phrase(p,"reliefpfeiler").
true 

?- phrase(p,"re").
false.

false

Posted 2017-02-20T01:34:20.470

Reputation: 131

2

CJam, 13 bytes

l_W%=e#e=%W_l

Explanation:

l_W%=e#e=%W_l
l_            e#Read input twice
  W%          e#Reverse one input
    =         e#Test for equality
     e#e=%W_l e#Comment to be a palindrome

Example:

> l_W%=e#e=%W_l
l_W%=e#e=%W_l
1

> l_W%=e#e=%W_l
Hi
0

> l_W%=e#e=%W_l
hh
1

Roman Gräf

Posted 2017-02-20T01:34:20.470

Reputation: 2 915

Try this: l_W%#e#%W_l – aditsu quit because SE is EVIL – 2017-02-26T18:47:06.800

2

J, 15 bytes, 15 characters

-:|.NB. .BN.|:-

Returns 1 if palindrome, 0 if not.

Output:

   f '())('
1
   f 'nope'
0

Explanation:

-:    NB. "Match" verb, checks for equality
|.    NB. Reverses the string

Blocks

Posted 2017-02-20T01:34:20.470

Reputation: 141

2

Ruby, 39 35 chars

->s{s.reverse==s}#}s==esrever.s{s>-

Dorian

Posted 2017-02-20T01:34:20.470

Reputation: 131

1Welcome to PPCG! Our usual policy (which is also explicitly affirmed in this challenge) is that answers must be either a full program (with input and output) or a function. I'm not super familiar with Ruby, but your code looks like a snippet instead (it's just an expression, and assumes input pre-stored in the s variable). If you change it so it's either a function or a full program, it will be a valid answer. – DLosc – 2017-02-23T10:47:45.017

@DLosc Sure, thanks, I made it a function – Dorian – 2017-02-23T10:55:39.997

1Then you can remove the () around s and save 4 bytes. – G B – 2017-02-23T10:57:07.630

2

Golfscript, 11 bytes

Bonus for the bytecount being a palindrome as well.

.-1%=#=%1-.

Duplicate, reverse, compare. Outputs 1 for palindromes and 0 for non-palindromes.

Josiah Winslow

Posted 2017-02-20T01:34:20.470

Reputation: 725

1

QBIC, 17 bytes

;?A=_fA|#|Af_=A?;

Uses a boring comment-like ttrick to make the code a palindrome. Explanation:

;         Get a string literal from the cmd prompt
?         Print -1 for true and 0 for false in the following comparison
A=        Is A equal to
_fA|      A reversed?

#         Start a 'silent' string literal: This only forces the creation of string B
          with the following text, but doesn't inject a reference to B$ here.
|Af_=A?;  Code, reversed, as a string literal.

steenbergh

Posted 2017-02-20T01:34:20.470

Reputation: 7 772

1

AppleScript, 146 bytes

clutch

set x to(display dialog""default answer"")'s characters--
x=reverse of x--x fo esrever=x
--sretcarahc s')""rewsna tluafed""golaid yalpsid(ot x tes

This should be fairly obvious.

Addison Crump

Posted 2017-02-20T01:34:20.470

Reputation: 10 763

1

Dyalog APL, 21 Bytes

I decided to avoid a comment-based solution, and ended up with something pretty ugly. Instead of commenting out the second half of my code, I keep it in and allow the resulting syntax error to be part of my output.

A←⍞⋄0∊A=⌽A⋄A⌽=A∊0⋄⍞←A

This prompts the user to enter a string, and prints

0
SYNTAX ERROR: The function requires a left argument
      A←⍞ ⋄ 0∊A=⌽A ⋄ A⌽=A∊0 ⋄ ⍞←A

If the input is a palindrome, and prints

1
SYNTAX ERROR: The function requires a left argument
      A←⍞ ⋄ 0∊A=⌽A ⋄ A⌽=A∊0 ⋄ ⍞←A

If the input is not a palindrome.

A simple comment based solution would be to replace the middle character () with the comment symbol ():

A←⍞⋄0∊A=⌽A⍝A⌽=A∊0⋄⍞←A

This does the same as above but doesn't include the syntax error in the output.

Here's an ungolfed version:

A←⍞          ⍝ prompt user for input, store in variable A
⋄             ⍝ statement separator 
0∊A=⌽A       ⍝ return '0' if A is equal to A reversed (`⌽A`). Otherwise return '1' 
⋄             ⍝ statement separator
A⌽=A∊0⋄⍞←A   ⍝ reverse of preceding code, throws a syntax error

ren

Posted 2017-02-20T01:34:20.470

Reputation: 189

1

[Python], 84 bytes

lambda s:list(s)==list(reversed(list(s)))#)))s(tsil(desrever(tsil==)s(tsil:s adbmal 

Pretty simple, take the string s, convert it into a list of chars and then compare that against the reversed list of chars generated from the same string.

sudobangbang

Posted 2017-02-20T01:34:20.470

Reputation: 111

This answer is invalid because it isn't a palindrome itself. – James – 2017-02-20T21:38:52.763

1@DJMcMayhem missed that caveat, but fixed now, unfortunately ugly. Helps clarify some of the other answers though ;P – sudobangbang – 2017-02-20T21:43:29.560

1

Common Lisp, 104 bytes

(lambda(s)(format t"~:[F~;T~]"(equal(reverse s)s)));;)))s)s esrever(lauqe("]~T;~F[:~"t tamrof()s(adbmal(

Abusing idea for commenting out part of code from comments under question.

user65167

Posted 2017-02-20T01:34:20.470

Reputation:

1

Javascript, 78 bytes

(s=>s==s.split('').reverse().join(''))//))''(nioj.)(esrever.)''(tilps.s==s>=s(

This is the classic .split, .reverse and .join routine.

Another (longer, but I like it more) 104 bytes solution would be

(s=>!s.split('').find((a,i,l)=>a!==l[l.length-i-1]))//))]1-i-htgnel.l[l==!a>=)l,i,a((dnif.)''(tilps.s!>=s(

svarog

Posted 2017-02-20T01:34:20.470

Reputation: 131

1

Pyth - 3 bytes

An alternative 3 byte solution. Palindromes give -1 and non-palindromes give 0.

_q_

Test Suite.

A few more 3 byte solutions:

_/_
_}_

Maltysen

Posted 2017-02-20T01:34:20.470

Reputation: 25 023

1

Scala - 58 bytes

def p(s:String)=s.reverse==s//s==esrever.s=)gnirtS:s(p fed

Example -

p("abba") //returns true

p("aabb") //returns false

Jyotman Singh

Posted 2017-02-20T01:34:20.470

Reputation: 111

1

Java (with regex), 134 bytes

s->s.matches("|(?:(.)(?<=(?=^.*?(\\1\\2?)$).*))+(?<=(?=^\\2$).*)")//)")*.)$2\\^=?(=<?(+))*.)$)?2\\1\\(?*.^=?(=<?().(:?(|"(sehctam.s>-s

Credits where due

Testing

import java.util.function.*;

class Ideone {
  static Predicate<String> isPalindrome = 
    s->s.matches("|(?:(.)(?<=(?=^.*?(\\1\\2?)$).*))+(?<=(?=^\\2$).*)")//)")*.)$2\\^=?(=<?(+))*.)$)?2\\1\\(?*.^=?(=<?().(:?(|"(sehctam.s>-s
    ;

  public static void main (String[] args) throws java.lang.Exception {
    testPalindrome("", true);
    testPalindrome("x", true);
    testPalindrome("xx", true);
    testPalindrome("xy", false);
    testPalindrome("xyx", true);
    testPalindrome("xxx", true);
    testPalindrome("xxyx", false);
    testPalindrome("racecar", true);
    testPalindrome("step on no pets", true);
    testPalindrome("aManaPlanaCanalPanaMa", true);
    testPalindrome("this is impossible", false);
  }

  static void testPalindrome(String s, boolean expected) {
    if (isPalindrome.test(s) == expected) {
      System.out.println("OK");
    } else {
      System.out.printf("NOK: str=\"%s\", expected=%b%n", s, expected);
    }
  }
}

Test it yourself!

Olivier Grégoire

Posted 2017-02-20T01:34:20.470

Reputation: 10 647

This code is not a lambda expression. – Jakob – 2017-08-30T00:22:48.217

@Jakob No? Why not? It works as a lambda, though. – Olivier Grégoire – 2017-08-30T05:39:09.233

My complaint is with the comment. Seems to me that having anything outside the lambda expression makes it an invalid lambda solution. In particular, this solution cannot be embedded in a program the way a lambda expression can (e.g. with code appearing after it on the same line). See also the grammar for a lambda expression, which most lambda solutions adhere to, but this doesn't.

– Jakob – 2017-08-30T20:41:02.367

Why can't it be embedded? stream.map(a->a//comment<new_line>).collect(...) (replace <new_line> with an actual new line) works well! Just because 99.99% of lambdas adhere to that principle doesn't make this one invalid. Does it? A lambda isn't defined from how it appears in the source, but how it's compiled, and this one compiles just fine, unless proven otherwise. – Olivier Grégoire – 2017-08-30T21:02:03.877

Well, my concern is not that it doesn't work or that it can't be embedded into a full program; it's that it violates the format of a lambda expression solution. I think I'll create a meta post where this discussion can continue. – Jakob – 2017-08-30T22:19:07.780

1

Scala, 50 bytes

Failed to not write a comment based solution so this is it:

(s:String)=>s.reverse==s//s==esrever.s>=)gnirtS:s(

sprague44

Posted 2017-02-20T01:34:20.470

Reputation: 81

1

Common Lisp, 61 bytes

(lambda(n)(equal(reverse n)n));))n)n esrever(lauqe()n(adbmal(

Try it online!

Outputs T when the string is palindrome, NIL otherwise.

Renzo

Posted 2017-02-20T01:34:20.470

Reputation: 2 260

1

Pyt, 1 byte

Try it online!

   implicit input
₽  palindromic test
   implicit output

Previous answer

before I knew that 1 byte solutions were allowed

₽ƥƥ₽

Try it online!

₽ checks for palindromicness (is that a word?)
ƥ prints the answer
ƥ prints nothing
₽ checks the palindromicness of nothing

FantaC

Posted 2017-02-20T01:34:20.470

Reputation: 1 425

11 byte. I don't think it ever says that solutions have to be more than a single byte. – caird coinheringaahing – 2018-01-28T21:09:14.903

Oh that's weird. I could have sworn it did, but maybe it was a different challenge. – FantaC – 2018-01-28T21:10:58.943

1

Japt, 2 bytes

êê

Try it

ê on its own in Japt creates a palindrome but, when passed a string as an argument (which the second ê is implicitly cast to in this case), it instead tests if a string is palindromic.

Shaggy

Posted 2017-02-20T01:34:20.470

Reputation: 24 623

1

Add++, 13 bytes

L,dbR=;=Rbd,L

Try it online!

caird coinheringaahing

Posted 2017-02-20T01:34:20.470

Reputation: 13 702

1

C (gcc), 118 bytes

Can sadly not come up with any solution without the boring comment approach.

f(char*s){char*t=s+strlen(s);for(;*s&&*s++==*--t;);s=!*s;}//};s*!=s;);t--*==++s*&&s*;(rof;)s(nelrts+s=t*rahc{)s*rahc(f

Try it online!

gastropner

Posted 2017-02-20T01:34:20.470

Reputation: 3 264

1

Reflections, 125 bytes

  _1=1\ /1#\ /+#_ 
      >~<  >~<
        (0//0)
      \ / >_ / _> / \      
)0//0(        
<~>  <~>      
 _#+/ \#1/ \1=1_  

Test it!

Prints code point 0x04 as true, and nothing as false. There are a few spaces after some lines.

Explanation

The relevant part of the code is:

  _1=1\ /1#\ /+#_
      >~<  >~<
        (0//0)
      \ / >_ /
  • _ reads a line
  • 1=1 copies it to stack 1
  • >~< creates a loop over the complete stack:
    • (0 pushes the top item to stack 0
  • now stack 0 contains the reverse, and stack 1 the original
  • 1 moves stack 1 to mainstack
  • >~< loops again:
    • 0) takes the next character from reversed
    • >_ if original and reversed character are not equal:
      • end the program by navigating downwards, leaving the field
  • + produces the 4
  • _ prints corresponding character
  • then the program stops (leaving the field to the right)

The rest is only for palindromic source. I know this is boring.

wastl

Posted 2017-02-20T01:34:20.470

Reputation: 3 089

1

Perl 5, 29 bytes

$_=reverse=~$_#_$~=esrever=_$

Requres -p0. Outputs 1 if the input is a palindrome, empty otherwise.

Try it online!


Perl 5, 33 bytes

+$_=reverse=~$_+q+_$~=esrever=_$+

Commentless version, requires -p0. Outputs 1 if the input is a palindrome, 0 otherwise.

Try it online!

primo

Posted 2017-02-20T01:34:20.470

Reputation: 30 891

1

Ruby -n, 31 bytes

p$_==$_.reverse#esrever._$==_$p

Try it online!

A full program with implicit input (-n) turns out to be slightly shorter than lambda.

Kirill L.

Posted 2017-02-20T01:34:20.470

Reputation: 6 693

1

R, 71 bytes

any(rev(x<-utf8ToInt(scan(,"")))-x)#)x-)))"",(nacs(tnIoT8ftu-<x(ver(yna

Try it online!

Comments to make it a palindrome. Returns FALSE if the input is a palindrome, TRUE otherwise. Could save 2 bytes off the palindrome using sd if we were guaranteed to have an input size >1.

Interpreting liberally this portion of the asnwer

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

would lead to:

R, 43 bytes

any(x<-scan()-rev(x))#))x(ver-)(macs-<x(yna

Try it online!

JayCe

Posted 2017-02-20T01:34:20.470

Reputation: 2 655

1

Dyalog APL, 6 bytes

⌽≡⊢⊢≡⌽

Try it online!

voidhawk

Posted 2017-02-20T01:34:20.470

Reputation: 1 796

1This is the "correct" answer (as it works on all arrays), but for this particular challenge specification (argument is always a string), ⌷≡⌽⌽≡⌷ or +≡⌽⌽≡+ works too while also being visually palindromic. – Adám – 2019-02-06T21:47:21.437

1

W, 3 bytes

Pretty trivial, reverse & check equality & reciprocal. (I definitely beat Jelly by tying with Pyth.)

_=_

Explanation

_   Reverse the implicitly inputted string
 =  Check whether this reversed string is equal to the original string
  _ Negate this string

user85052

Posted 2017-02-20T01:34:20.470

Reputation:

0

SmileBASIC, 109 bytes

INPUT P$WHILE LEN(P$)>1P=P+(POP(P$)!=SHIFT(P$))WEND?!P'P!?DNEW))P$(TFIHS=!)$P(POP(+P==P1>)$P(NEL ELIHW$P TUPNI

Could definitely be shorter...

12Me21

Posted 2017-02-20T01:34:20.470

Reputation: 6 110

0

Perl 5, 69 bytes

$_=<>;chop;print 1 if reverse eq$_#_$qe esrever fi 1 tnirp;pohc;><=_$

Takes input via STDIN. Prints 1 if the input is a palendrome, otherwise exits.

tucker twomey CodeZypher

Posted 2017-02-20T01:34:20.470

Reputation: 11

A couple of hints: you could save quite a few bytes by just printing the boolean returned by eq directly; and you could request input with no trailing newline to avoid the need to call chop. – None – 2017-03-28T22:32:36.170

0

REXX, 78 bytes

arg a
b="say a=reverse(a)"
interpret b
exit
tixe
b terpretni
")a(esrever=a yas"=b
a gra

I thought that early exit would leave the mirrored program uninterpreted, but at least Regina REXX complains about unbalanced parentheses, hence the INTERPRET workaround.

idrougge

Posted 2017-02-20T01:34:20.470

Reputation: 641

0

Kotlin, 32 bytes

Just another "boring" solution but in Kotlin :))

s.reversed()==s//s==)(desrever.s

Try it online!

Valentin Michalak

Posted 2017-02-20T01:34:20.470

Reputation: 211

0

RProgN 2, 5 bytes

]iei]

Returns -1\n-1 for true, 0\n0 for false.

Explained

]iei]
]       # Duplicate the top of the stack.
 i      # Reverse the duplicate
  e     # Are they equal? 1 if true, 0 if false.
   i    # Inverse calculates n*-1. Redunant.
    ]   # Duplicate. Redundant.

Try it online!

ATaco

Posted 2017-02-20T01:34:20.470

Reputation: 7 898

0

BASH, 52 bytes

[ "`rev<<<$1`" = "$1" ];$?$;] "1$" = "`1$<<<ver`" [

success output (run as palpal.bash:

~/workspace/golf $ bash palpal.bash "$(cat palpal.bash)"
palpal.bash: line 1: 0$: command not found
palpal.bash: line 1: 1$: command not found
palpal.bash: line 1: ]: command not found

failure output:

~/workspace/golf $ bash palpal.bash "hello"
palpal.bash: line 1: 1$: command not found
palpal.bash: line 1: 1$: command not found
palpal.bash: line 1: ]: command not found

notice that first output line has either a 1 or 0 which are the return code of the actual palindrome check.

It's not much better than using a comment for the second half

JoshRagem

Posted 2017-02-20T01:34:20.470

Reputation: 189

0

C++ (gcc), 138 bytes

[](auto s){return equal(s.begin(),s.begin()+s.size()/2,s.rbegin());}//};))(nigebr.s,2/)(ezis.s+)(nigeb.s,)(nigeb.s(lauqe nruter{)s otua(][

Lambda using a builtin to check if the argument is palindromic. To make it palindromic, I had to add the entire source, reversed, in a comment. Not very creative, I know. Note that clang will not compile this.

Try it online here.

O.O.Balance

Posted 2017-02-20T01:34:20.470

Reputation: 1 499

0

Gol><>, 31 bytes

iEv
?;>l2(q1h{-
-{h1q(2l>;?
vEi

Try it online!

iEv
?;>l2(q1h{-

iEv
i    Take input as a char
 E   If the last input was EOF, pop and execute next; otherwise, skip 1
iEv  Take all input and move to next line

>l2(q1h{-?;
 l2(         Is the stack length < 2? (1 if yes, 0 otherwise)
    q        If not, skip 2 commands (consume the top)
     1h      Print 1 and halt
       {     Rotate the stack (bottom goes to the top)
        -    Difference
         ?   If zero, skip one command (consume the top)
          ;  Halt without printing anything

Bubbler

Posted 2017-02-20T01:34:20.470

Reputation: 16 616

0

C# (.NET Core), 110 bytes

s=>Console.Write(new string(s.Reverse().ToArray())==s)//)s==))(yarrAoT.)(esreveR.s(gnirts wen(etirW.elosnoC>=s

Try it online!

Anderson Pimentel

Posted 2017-02-20T01:34:20.470

Reputation: 213

0

Jelly, 3 bytes

UƑU

Try it online!

Added for the sake of completeness. This uses the fairly new quick Ƒ, which checks whether the argument is the same as the result of the atom being applied. Here, the atom is U, which reverses the argument. The quicklink returns either 1 for palindromes, or 0 otherwise. The second U has no effect on either integer, so leaves it as is.

user81156

Posted 2017-02-20T01:34:20.470

Reputation:

0

Pascal (FPC), 139 bytes

uses strutils;var s:string;begin read(s);write(s=ReverseString(s))end.dne))s(gnirtSesreveR=s(etirw;)s(daer nigeb;gnirts:s rav;sliturts sesu

Try it online!

The program compares if the input is equal to its reverse using ReverseString() function from module strutils.

end. is the last part of the program. This program does not use comments because, in most cases, FPC does not care what is after end., so the program without the dot is simply reversed and appended to the program itself.

AlexRacer

Posted 2017-02-20T01:34:20.470

Reputation: 979

0

T-SQL, 70 bytes

SELECT IIF(s=REVERSE(s),1,0)FROM t--t MORF)0,1,)s(ESREVER=s(FII TCELES

Didn't see a SQL version, so here's my try. Returns 1 for a palindrome, and 0 otherwise. IIF is supported by SQL 2012 and later.

Input is via pre-existing table t with varchar field s, per our IO rules. The input table can include multiple rows.

The -- in the middle is a comment character.

Edit: Here's a more clever version, using a strangely-named input table instead of an extended comment. Unfortunately slightly longer (73 bytes):

--"
SELECT IIF(s=REVERSE(s),1,0)FROM"MORF)0,1,)s(ESREVER=s(FII TCELES
"--

This requires an input table with a long strange name:

CREATE TABLE [MORF)0,1,)s(ESREVER=s(FII TCELES
] (s VARCHAR(999))

Note the return is included in the table name (not a recommended naming strategy for your databases, by the way).

Tables with otherwise invalid characters in their name need brackets or double-quotes around them, and the only way I could put the required quote at the beginning was to set it after a comment.

BradC

Posted 2017-02-20T01:34:20.470

Reputation: 6 099

0

C# (.NET Core), 94

s=>Console.Write(s.SequenceEqual(s.Reverse()))//)))(esreveR.s(lauqEecneuqeS.s(etirW.elosnoC>=s

Improves slightly on the answer from Anderson Pimentel

Try it online

ryzngard

Posted 2017-02-20T01:34:20.470

Reputation: 11

0

MUMPS, 36 bytes

r r r w r=$re(r) ; )r(er$=r w r r r

Explanation

There are no reserved keywords in MUMPS; the compiler just uses context to decide if you you are referencing a routine, command, or variable.

r : define routine "r"

r r : read input and assign to variable "r"

w r=$re(r) : write 1 if "r" equals its reverse ($re is the shortened form of $reverse)

; )r(er$=r w r r r : lame comment to make palindromic

Ungolfed version:

palindromeRoutine
 read input
 write input=$reverse(input)

João the Clown

Posted 2017-02-20T01:34:20.470

Reputation: 31