16
2
Write a full program to find whether the binary representation of a number is palindrome or not?
Sample Input
5
Sample Output
YES
Print YES
if binary representation is palindrome and NO
otherwise.
16
2
Write a full program to find whether the binary representation of a number is palindrome or not?
Sample Input
5
Sample Output
YES
Print YES
if binary representation is palindrome and NO
otherwise.
5
~2base.-1%="YES""NO"if
24
n=bin(input())[2:]
print'YNEOS'[n!=n[::-1]::2]
Wow. What does [n!=n[::-1]::2]
do? – Wile E. Coyote – 2011-02-08T21:38:47.257
2@Dogbert, n[::-1] is a slice. The start and end indexs are empty, so it means the whole string. The stepsize is -1, so when you see [::-1] it is a short way to reverse a string/list etc. So n!=n[::-1] is True (ie 1) when n is not a palindrome. Therefore when n is a palindrome, you get 'YNEOS'[0::2] - start at 0 and take every 2nd character. When n is not a palindrome you get 'YNEOS'[1::2] - start at 1 and take every second character :) – gnibbler – 2011-02-08T22:22:26.570
I think people are voting for the slice trick :), rightly so. :P +1 – st0le – 2011-02-09T06:08:47.557
4
$><<%w(YES NO)[(n="%b"%$*)<=>n.reverse]
Thanks to Michael Kohl's "%b"%gets trick.
Very nice, I like this a lot! +1 for using the spaceship in a creative way :-) – Michael Kohl – 2011-03-06T17:23:01.523
4
C 84 81 74 Characters
r;main(v,x){for(scanf("%d",&v),x=v;v;v/=2)r=r*2|v&1;puts(r-x?"NO":"YES");}
It does not use any function like string reverse.
Couldn't you save a few characters changing r<<=1
into r*=2
, v>>=1
into v/=2
and {}
into ;
? – None – 2016-09-23T13:20:26.033
@paxdiablo Indeed. Changed. Thanks a lot. – fR0DDY – 2016-09-24T07:48:54.070
r*=2,r|=v&1
-> r=r*2|v&1
(-2) – Titus – 2016-09-24T13:00:30.200
and moving that term to the body of the loop saves another byte. – Titus – 2016-09-24T13:01:44.933
3
alert((a=(prompt()*1).toString(2))-a.split("").reverse().join("")?"NO":"YES")
More information
prompt()*1
: Quick trick to convert string to number.
.toString(2)
: That's how you convert to binary in javascript.
a.split("").reverse().join("")
: There is no native support to reverse string, so you have to convert string to array and array to string.
("[part1]" - "[part 2]")?"YES":"NO"
: -
is a replacement for !=
to save 1 char.
1Excellent explanation. – TehShrike – 2011-03-03T01:44:00.300
2
('NO','YES')[($a=[Convert]::ToString("$input",2))-eq-join$a[64..0]]
2
<?=strrev($n=decbin(`cat`))==$n?@YES:@NO;
Test:
php 713.php <<< 5
YES
php 713.php <<< 6
NO
4If you're going to use shell calls to get the input, might as well use m4
instead of cat
to save one. There's also pg
and dd
(which writes some bytes to stderr). – Nabb – 2011-02-08T15:34:49.827
Have You tried that on Windows? ;) – Titus – 2016-09-24T13:19:24.147
2
$_=sprintf'%b',shift;
print reverse==$_?YES:NO
2
Ruby, 43 characters
puts((n="%b"%gets)==n.reverse ? "YES":"NO")
Save 2: puts (n="%b"%gets)==n.reverse ? :YES: :NO
– Phrogz – 2011-03-02T16:15:30.633
2
Hey nice! I tried to golf it a bit and came to 12 bytes ‘NO…Ü‘#EbÂQè
:).
Great! I still don't know how to use/make compressed strings. Also, I didn't know the function bin()
existed – acrolith – 2016-09-25T19:47:51.140
2
There is actually a detailed example here, if you are interested :).
– Adnan – 2016-09-25T20:05:27.207This answer is non-competing since the question predates the language. – Okx – 2017-02-02T20:08:52.167
1
BṚ⁼Bị“YES“NO
Explanation:
BṚ⁼Bị“YES“NO Main link. Arguments: z.
B Binary representation of z.
Ṛ Reversed.
B Binary representation of z.
⁼ Check if x is equal to y.
“YES“NO [['Y', 'E', 'S'], ['N', 'O']]
ị xth element of y (1-indexed).
Before printing, Python's str
function is mapped through a list, and then the elements are concatenated, so you see YES
or NO
.
1
1
n=bin(input())[2:]
print'YES'if n==n[::-1]else'NO'
You can ['NO','YES'][n==n[::-1]]
– Karl Napf – 2016-11-03T15:35:26.703
1
No string reverse:
print f(split//,sprintf'%b',shift);
sub f{@_<=1?YES:shift!=pop()?NO:f(@_)}
1
This one constructs all palindromes up to 2^32.
sub f{
my($x,$l)=@_;
$l+=2,f(($x<<$_)+1+(1<<$l-1),$l)?return 1:0 for 1..15-$l/2;
$x-$ARGV[0]?0:1
}
print f(0,1)+f(0,0)+f(1,1)?YES:NO
1
C=`dc<<<$1\ 2op`;[ $C = `rev<<<$C` ]&&echo YES||echo NO
Well, technically that's bash
and dc
and rev
:-) – None – 2016-09-23T13:26:34.877
1
0?k=n;n?k=div n 2?(n`mod`2+k*2);f x|x==x?0="YES"|True="No";main=interact$f.read
Don't forget: In Haskell, this will work with really big numbers. – FUZxxl – 2011-02-08T21:13:07.183
2Ahm, that's actually 79 characters. ;-) – Michael Kohl – 2011-02-08T22:49:57.463
1
13 : ';(]-:|.)#:y{''YES'';''NO'''
1
((-:|.)#:x){2 3$'NO YES'
eg:
((-:|.)#:5){2 3$'NO YES'
YES
((-:|.)#:12){2 3$'NO YES'
NO
((-:|.)#:125){2 3$'NO YES'
NO
((-:|.)#:63){2 3$'NO YES'
YES
1
%2>"YNEOS"!qJ.BQ_J
Also 18 bytes:
@,"NO""YES"qJ.BQ_J
1
I wanted to do it without using strings at all.
iterative solution, 78 bytes
for($x=log($n=$argv[1],2);$i<$x&($n>>$i^$n>>$x-$i^1);$i++);echo$i<$x/2?NO:YES;
recursive solution, 113 bytes
function p($n,$x=0){return$n<2?$n:is_pal(($n&(1<<$x=log($n,2)/2)-1)^$n>>$x+!is_int($x));}echo p($argv[1])?YES:NO;
If n
is a binary palindrome, the upper half xor the lower half is also a binary palindrome and vice versa.
a port of the excellent C answer from fR0DDY, 58 bytes
for($x=2*$v=$argv[1];$x/=2;$r=$r*2|$x&1);echo$r-$v?NO:YES;
a binary reverse. Columbus´ egg.
1
Byte count assumes ISO 8859-1 encoding.
.+
$*
+`(1+)\1
${1}0
01
1
^((.)*?).??((?<-2>.)*$)
$1¶$3
O$^`.(?=.*¶)
^(.*)¶\1
Convert to unary. Convert that to binary. Cut the number in half and remove a middle digit if there is one. Reverse the first half. Match if both halves are equal.
0
Works only with system platforms (php, neko, cpp, etc.). Takes input through command line arguments.
class T{static function main(){var r:Dynamic=Std.parseInt(Sys.args()[0]);var s=r.toString(2);trace(s==[for(i in-s.length+1...1)s.charAt(-i)].join('')?"YES":"NO");}}
0
function paly(n)
v=de2bi(n);
if v==v(numel(v):-1:1)
'YES'
else
'NO'
end
1Welcome to PPCG! – Martin Ender – 2016-09-24T17:56:57.513
-1
Java, 97 85 characters
return Integer.toBinaryString(i).equals(new StringBuffer(s).reverse()+"")?"YES":"NO";
String s=Integer.toBinaryString(i); return s.equals(new StringBuffer(s).reverse()+"")?"YES":"NO";
2The task calls for a full program. – Joey – 2011-03-02T14:32:40.400
What should be the output when it's not a palindrome? – Wile E. Coyote – 2011-02-08T15:20:14.630
@dogbert It should be 'NO' without the quotes. – fR0DDY – 2011-02-08T15:22:11.997
How do you know it's a palindrome? Because the values from the first nonzero to the end of the "string" are palindromic? This smells really bad to me, as a challenge. – jcolebrand – 2011-02-09T23:40:09.647
1Much as I <3 gnibbler's answer, it's not actually the shortest solution, and any question tagged [code-golf] should pick the shortest solution as the winner. – Chris Jester-Young – 2011-02-10T03:48:46.893
Input is given how? – Joey – 2011-03-02T08:34:51.020
@Joey Standard Input – fR0DDY – 2011-03-02T13:58:47.247