# Tag Info

## Hot answers tagged palindrome

56

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.

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.

41

CJam, 45 bytes 0{{)_s:C,2m*{~Ce\is_W%=},,2/:O!CCW%=|}g}ri*SO Try it online! How it works 0 e# Push 0 (candidate). { e# Loop: { e# Loop: )_ e# Increment the candidate and push a copy. s:C e# Cast to string and save in C. , e# Get the length of C, i.e., the number of digits. 2m* e# ...

34

GolfScript, 107 91 .4:ab-1:ba=;1 %ba%{...fi@@= c43.=;)('"([{ }])"'~?~'"([{ }])"')(;=.34c =@@if...}%ab% 1;=ab:1-ba:4. Newlines are artistic. fi, c43 and c are noops, but the entire code is executed. Prints -3-1-1 for convenient palindromes, -4-1-1 otherwise. Try it online! Alternate version, 155 bytes At the cost of 64 bytes, this can be improved upon: ...

34

///, 37 bytes Greetings, Planet!/!tenalP ,sgniteerG

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!

31

05AB1E, 10 7 bytes Code: Â'š×Â‡Q Explanation: To check if a string is a palindrome, we just need to check the input with the input, with at swapped and cg swapped and then reverse it. So that is what we are going to do. We push the input and the input reversed using Â (bifurcate). Now comes a tricky part. 'š× is the compressed version for creating. If we ...

30

Python 2, 48 47 46 bytes lambda s:s[~len(s)/2:]in'987654321'>s==s[::-1] Test it on Ideone. How it works In Python, a chained comparison returns True if and only if all individual comparison do the same. In this specific case, our lambda returns True if and only if all of the following conditions are met. s[~len(s)/2:]in'987654321' For a string s of ...

29

K, 25 {x~|x:_x@&x in,/.QaAn} . k){x~|x:_x@&x in,/.QaAn}"Eva, can I stab bats in a cave?" 1b

27

Dyalog APL, 6 4 ⌽,,⌽ Try it here. Other solutions: ⌽,⊢⊢,⌽ ⌽⊢⊢,⊢⊢⌽ Explanation They are just: {⌽((,⍵),(⌽⍵))} {⌽((,⍵)⊢((⊢⍵),(⌽⍵)))} {(⌽⍵)⊢((⊢⍵),((⊢⍵)⊢(⌽⍵)))} Monadic , and ⊢ does nothing on strings. Dyadic , is concatenation. Dyadic ⊢ returns its right operand. And ⌽ is obviously reversion.

24

Perl, 26 char s/_|\W//g;uc eq reverse uc Evaluates to 1 when $_ is a palindrome, "" (one of Perl's false values) when it is not. Sample usage: sub palin { s/_|\W//g;uc eq reverse uc } while (<DATA>) { chomp; print "$_ => ",palin()?"yes":"no","\n"; } __DATA__ Eva, can I stab bats in a cave? A man, a plan, a canal. Panama! Madam, I'm ...

24

Jelly, 5 bytes ⁼ṚaṚ⁼ Try it online! Equals reverse and reverse equals. Or the more efficient yet less aesthetically pleasing: ⁼Ṛ Ṛ⁼ or Ṛ⁼ ⁼Ṛ

23

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

22

piet 19x2 = 38 Accepts input until it encounters 0x00. Doesn't terminate, but output will be correct.

22

Perl, 39 37 42 39 + 1 = 40 bytes Using a new method, I managed to cut down a huge number of bytes. Run with the -n flag. Accepts input repeatedly at runtime, printing 0 or 1 accordingly. I had to add 5 bytes because I realized without it, the code worked for inputs such as 1234567900987654321, which is not a Giza number. Since Giza numbers never contain ...

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.

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!

19

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!

19

Brachylog, 7 bytes ~+ℕᵐ.↔ᵐ Try it online! Surprisingly not that slow. Explanation (?)~+ . Output is equal to the Input when summed ℕᵐ. Each element of the Output is a positive integer .↔ᵐ(.) When reversing each element of the Output, we get the Output

18

Pyth, 17 bytes Z.{rzZ.q.Zzr}.Z Try it online here. The leading space is necessary. I have counted it and the trailing space in the byte count. Here's the breakdown: z z is initialized to the input r Z Z is initialized to 0, and r(string)0 converts the string to lowercase .{ .{ is pyth's builtin uniqueness test ...

18

Jelly, 9 bytes O%8µ+U5ḍP Try it online! or verify all test cases. How it works O%8µ+U5ḍP Main link. Argument: S (string) O Compute the code points of all characters. %8 Compute the residues of division by 8. This maps 'ACGT' to [1, 3, 7, 4]. µ Begin a new, monadic link. Argument: A (array of residues) +U Add ...

18

Jelly, 11 bytes ŒufØAe€ØCŒḂ Try it online! Alternate versions: ŒlfØae€ØCŒḂ ŒufØAe€ØcŒḂ ŒlfØae€ØcŒḂ Of course a challenge appreciating Dennis must have an answer in a language of his.

17

C# 82 only :) var x=s.ToLower().Where(char.IsLetterOrDigit);return x.SequenceEqual(x.Reverse()); Couldn't resist the temptation of writing a boilerplate-free program in my favorite language. A test is available here: http://ideone.com/8bwz7z

17

Perl, 55 54 bytes undef$/;$a=<>."\n__END__\n";print$a,scalar reverse$a; Reads program source from stdin and writes to stdout. Result of running on itself: undef$/;$a=<>."\n__END__\n";print$a,scalar reverse$a; __END__ __DNE__ ;a$esrever ralacs,a$tnirp;"n\__DNE__n\".><=a$;/$fednu

17

APL, 9 ⍞←Z,⌽,Z←⍞ Explanation: Z←⍞ ⍝ read a line from the keyboard, and store it in Z , ⍝ flatten into one-dimensional array (this has no effect here) ⌽ ⍝ reverse Z, ⍝ concatenate Z to its reverse ⍞← ⍝ explicit output (not necessary, but it makes it a palindrome)

17

Python 3, 125 The main problem is to make the reverse of the code parsable. Then we can let it error out from undefined identifiers. w=[str.lower][0]((input)()) (print)((max)((map)(w.count,w))<2) (2>((w,tnuoc.w)(pam))(xam))(tnirp) (()(tupni))[0][rewol.rts]=w

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

17

05AB1E, 9 bytes žM¹álSåÂQ Try it online! -2 thanks to Adnan. This attacks Jelly's pain point exactly. It uses l and A, 1-byte equivalents for Jelly's Œl and Øa respectively.

17

05AB1E, 7 bytes b0Ü‚DíQ Try it online! or as a Test Suite Explanation b # convert input to binary 0Ü # remove trailing zeroes ‚ # pair with input D # duplicate í # reverse each (in the copy) Q # check for equality

Only top voted, non community-wiki answers of a minimum length are eligible