Defintion
A centrosymmetric matrix is a square matrix that is symmetric about its center. More rigorously, a matrix \$A\$ of size \$n \times n\$ is centrosymmetric if, for any \$i,\: j \in ([1, n] \cap \mathbb{Z})\$ the following relation is satisfied: $$A_{i,\:j}=A_{n+1-i,\:n+1-j}$$
Examples of such matrices
Here is an illustration of the symmetry of a matrices like these (borrowed from the aforementioned Wikipedia article):
Centrosymmetric 5 by 5 matrix illustrated in a really nice way
An even-side-length (\4ドル\times 4\$) centrosymmetric matrix:
$$\left(\begin{matrix} 1 & 2 & 3 & 4 \\ 5 & 6 & 7 & 8 \\ 8 & 7 & 6 & 5 \\ 4 & 3 & 2 & 1\end{matrix}\right)$$
And an odd-side-length (\3ドル\times 3\$) one:
$$\left(\begin{matrix} 1 & 2 & 3 \\ 5 & 6 & 5 \\ 3 & 2 & 1\end{matrix}\right)$$
Task and Specs
Given a square matrix of size at least \2ドル\,ドル output one of two distinct and consistent values, deciding whether the matrix is centrosymmetric or not. You can assume that the matrix will consist entirely of positive integers.
However, your code must also be centrosymmetric. That is, it must be a program / function (or equivalents) consisting of \$n\$ lines, each of which containing \$n\$ bytes in your language's encoding, and must satisfy the definition given above, but with bytes instead of positive integers. Your submission's score will be the value of \$n\,ドル with a lower \$n\$ being better.
You can take input and provide output through any standard method and in any reasonable format, while taking note that these loopholes are forbidden by default. You may (optionally) choose to take the size, \$n\,ドル as input too (unless you take input as a 1D list, in which case you may only take \$n^2\$ as additional input).
Test cases
Truthy:
[[1, 2], [2, 1]]
[[1, 2, 3], [5, 6, 5], [3, 2, 1]]
[[10, 5, 30], [2, 6, 2], [30, 5, 10]]
[[100, 100, 100], [100, 50, 100], [100, 100, 100]]
[[1, 2, 3, 4], [5, 6, 7, 8], [8, 7, 6, 5], [4, 3, 2, 1]]
[[3, 4, 5, 6, 7], [5, 6, 7, 8, 9], [3, 2, 10, 2, 3], [9, 8, 7, 6, 5], [7, 6, 5, 4, 3]]
Falsy:
[[1, 2], [1, 2]]
[[1, 2, 10], [5, 6, 5], [11, 2, 1]]
[[14, 5, 32], [2, 6, 2], [30, 5, 16]]
[[19, 19, 19], [40, 50, 4], [19, 19, 19]]
[[1, 2, 20, 4], [7, 6, 7, 8], [8, 7, 6, 6], [3, 3, 2, 1]]
[[3, 4, 5, 6, 7], [5, 6, 7, 8, 9], [4, 5, 10, 4, 5], [5, 6, 7, 8, 9], [3, 4, 5, 6, 7]]
28 Answers 28
JavaScript (ES6), size (削除) 12 (削除ここまで) (削除) 11 (削除ここまで) 9
All versions return false for centrosymmetric or true for non-centrosymmetric.
1-dimensional array + length, size 9 (89 bytes)
Takes input in currying syntax (length)(array), where array is 1-dimensional.
w=>a=> //
a.some //
(v=>v- //
a[--w])//
/////////
//)]w--[a
// -v>=v(
// emos.a
// >=a>=w
Matrix + width, size 11 (131 bytes)
Takes input in currying syntax (width)(matrix).
/**/w=>a=>a
.some(r=>r.
some(v=>v-a
[y][w-++x],
x=!y--),y=w
);'*/'/*';)
w=y,)--y!=x
,]x++-w[]y[
a-v>=v(emos
.r>=r(emos.
a>=a>=w/**/
Payload
w => a => a.some(r => r.some(v => v - a[y][w - ++x], x = !y--), y = w)
Container
/**/CODE;'*/'/*';EDOC/**/
Matrix only, size 12 (155 bytes)
This is my original solution, which computes the width of the matrix by itself.
/**/a=>a[w=a
.length-1,s=
'some']((r,y
)=>r[s]((v,x
)=>v-a[w-y][
w-x]))////*/
/*////))]x-w
[]y-w[a-v>=)
x,v((]s[r>=)
y,r((]'emos'
=s,1-htgnel.
a=w[a>=a/**/
How?
We need a few critical tokens that cannot be split:
somelength)=>
Line-feeds may be inserted almost anywhere else.
The unrolled payload code reads as:
a => a[w = a.length - 1, s = 'some']((r, y) => r[s]((v, x) => v - a[w - y][w - x]))
and is wrapped inside the following structure:
/**/CODE////*/
which, once reversed, becomes a valid block comment:
/*////EDOC/**/
Befunge-93, size 24
&:00p110p920p::*:v
vp01:+1g01-1pg02g01&_v#<
>00g`#v_>: 1$^
v2p011< v:g00p029p01< @
>0g1+20p^>*:#v_1# .#<@ .
v+1g00gg02g01<$ >1+10pv0
>:10g-88円++20g -g- |
vg02p011_v#`g00g01-1 <1
>1+20p >: ^ ^ <
< ^ ^ :> p02+1>
1< 1-10g00g`#v_110p20gv
| -g- g02++88\-g01:>
0vp01+1> $<10g20gg00g1+v
. @<#. #1_v#:*>^p02+1g0>
@ <10p920p00g:v <110p2v
^1ドル :>_v#`g00>
<#v_&10g20gp1-10g1+:10pv
v:*::p029p011p00:&
Input: n, followed by the elements of the array, all separated by spaces. NOTE: you may need to use a different interpreter if you have a large enough input.
I'm sure there's a better way to do this, I just wanted to try this in Befunge. The actual code portion is the top half.
How?
The code is divided into two main sections, the initialization and the verification.
Initialization:
&:00p110p920p::*:v
vp01:+1g01-1pg02g01&_v#<
>00g`#v_>: 1$^
This section of the code writes the input matrix right below the code as ASCII characters. Both this and the next section use the three cells at the top left of the code as data. They are stored as n, i, j.
Verification:
v
1
v2p011< v:g00p029p01< @
>0g1+20p^>*:#v_1# .#<@ .
v+1g00gg02g01<$ >1+10pv0
>:10g-88円++20g -g- |
vg02p011_v#`g00g01-1 <1
>1+20p >: ^ ^ <
This sections checks every index \$i, j\$ [1-indexed] against the condition \$ A_{i, j} = A_{n+1-i, n+1-j} \$. However, we have a problem: we have \$j\$ offset by \8ドル\$. To fix that, the following formulae are used:
\$ i' = (n + 1) - i \$
\$ j' = n + 1 - (j - 8) + 8 = (n + 1) + (8 + 8) - j\$
The other portions of code are unread garbage to make it centrosymmetric.
Haskell, \$n = 8\$
No comments!
Takes input as a 1-D list
s a b=
a/=b
f =s<*>
reverse
esrever
>*<s= f
b=/a
=b a s
\$n= 10\$
Takes input as a 2-D matrix
r =reverse
s a b=a/=b
f =s<*>r
c =concat
g = f<$>c
c>$<f = g
tacnoc= c
r>*<s= f
b=/a=b a s
esrever= r
Thanks to potato44 for all their help in chat. And Lynn for golfing off a row.
Explanation
The general idea here is a simple one, we concat the list and compare it to it's reverse. However since we want to be centrosymmetric we have to tread carefully. First we write the code as we would normally:
g=((==)<*>reverse).concat
Now in order for our reverse lines to also be valid Haskell we need the left hand side of our equations to look like a function definition tacnoc.)esrever>*<)==(( doesn't.
The first step to fixing this is disposing of parentheses.
s a b=a==b
f=s<*>reverse
g=f.concat
However we have some new problems now. Both . and == when reversed are the same, so our reversed lines attempt to redefine the operators (<*> reversed is >*< so we are fine on that front). . can be replaced with <$> since functions are functors. And we can replace == with /=, which negates our output, but that is still within the specs. Now we have
s a b=a/=b
f=s<*>reverse
g=f<$>concat
In order to trim down our line length we alias concat and reverse.
r=reverse
s a b=a/=b
f=s<*>r
c=concat
g=f<$>c
Now we just finish this by making everything centrosymmetric and square.
The shorter 1-D version works in much the same way except since there is no need to concat we can save two lines by removing it.
-
-
\$\begingroup\$ @Lynn Thanks! In my head the reverse of
<$>was also<$>. \$\endgroup\$2018年07月17日 17:32:59 +00:00Commented Jul 17, 2018 at 17:32
Python 2, size 10 (109 bytes)
def f(a):#
return(a#
==[r[::-1#
]for r in#
a[::-1]])#
#)]]1-::[a
#ni r rof]
#1-::[r[==
#a(nruter
#:)a(f fed
Previous Python 2, size 14 (209 bytes)
lambda a:all(#
a[i][j]==a[-i#
-1][-j-1]for #
i in range( #
len(a))for j #
in range(len(#
a))) #
# )))a
#(nel(egnar ni
# j rof))a(nel
# (egnar ni i
# rof]1-j-[]1-
#i-[a==]j[]i[a
#(lla:a adbmal
-
1\$\begingroup\$ Not sure if it would be enough for a rearrangement but FYI
-i-1and-j-1could simply be~iand~j\$\endgroup\$Mr. Xcoder– Mr. Xcoder2018年07月16日 21:57:50 +00:00Commented Jul 16, 2018 at 21:57 -
\$\begingroup\$ @Mr. Xcoder: I fiddled with that a bit, but it doesn't change the outcome. \$\endgroup\$Chas Brown– Chas Brown2018年07月16日 22:15:32 +00:00Commented Jul 16, 2018 at 22:15
-
\$\begingroup\$ Works in python3.6 too, but might not be optimal. \$\endgroup\$SIGSTACKFAULT– SIGSTACKFAULT2018年07月17日 15:15:56 +00:00Commented Jul 17, 2018 at 15:15
Pyth, size 6 (41 bytes)
q_M_QQ
q.q
q.q
QQ_M_q
Explanation
The first line reverses the input and each row, and checks if we're left with a copy of the input.
The .q in the second line exits the program, so everything after that, as well as the q before it, is a no-op.
APL (Dyalog Unicode), score (削除) 7 (削除ここまで) (削除) 6 (削除ここまで) (削除) 5 (削除ここまで) 4
Full program. Prompts for matrix expression from stdin. Prints a 1 or a 0 and then throws a consistent error. No comments!
r←,⎕
r≡⌽r
r⌽≡r
⎕,←r
⎕ prompt for expression and evaluate it
, ravel (flatten) it
r← store the result in r (for ravelled)
⌽r mirror r
r≡ implicitly print whether r is identical to that
≡ depth of r (gives 1)
r⌽ use r to rotate that
(this will always cause a RANK ERROR and quit because a matrix is not permitted as left argument to ⌽)
Perl 6, size (削除) 9 8 (削除ここまで) 7
{.flat#
#}talf.
# ],R[
eq # qe
[R,] #
.flat}#
#talf.{
(削除) Okay, I've changed it to reverse is the bottleneck here. I've changed it to the technically longer, but much more splittable [*-1 X-^*]. (削除ここまで)[R,] after seeing Brad Gilbert's answer to another question.
The flattened code is {.flat eq [R,] .flat}.
Edit: Damn, I've realised I don't even need to flatten the first list, but attempts at shortening it to a size 6 have failed with just one byte too many to fit...
{@_#}_@
eq#<<|
[R,] #
# ],R[
|<<#qe
@_}#_@{
:(
Though if we take it as a one dimensional array, it's easy to fit it in a size 6.
{$_# }
eq #_$
[R,] #
# ],R[
$_# qe
} #_${
Wolfram Language (Mathematica) (REPL only), size 8 (71 bytes)
f=#[[c=-
1;;1;;-1
,c]]==#&
(*
*(
&#==]]c,
1-;;1;;1
-=c[[#=f
Returns True for centrosymmetric input, and False otherwise.
On Mathematica REPL, lines with syntax errors are ignored (with a thick red bar appearing on the side of the screen). Here, only the first three lines, f=#[[c=-1;;1;;-1,c]]==#&, are executed.
This code captures the function in the name f.
Sadly, the built-in PalindromeQ is too long.
-
\$\begingroup\$ ._. well, this isn't centrosymmetric according to the challenge: \$ n>=2 \,ドル but nice job. \$\endgroup\$Adalynn– Adalynn2018年07月17日 13:57:59 +00:00Commented Jul 17, 2018 at 13:57
-
6\$\begingroup\$ By my understanding the \$n \geq 2\$ is a constraint on the input, not on the definition of a centrosymmetric matrix. \$\endgroup\$lynn– lynn2018年07月17日 14:05:15 +00:00Commented Jul 17, 2018 at 14:05
-
\$\begingroup\$ Whoops, forgot that. \$\endgroup\$Adalynn– Adalynn2018年07月17日 15:49:00 +00:00Commented Jul 17, 2018 at 15:49
Japt, size (削除) 9 (削除ここまで) 4 (11 bytes)
UaSê
êSaU
Takes one-dimensional input, checks if it is a palindrome. If you want, you can fill the two empty lines for some ascii art.
The old two-dimensional version, rotates the input twice and checks if it's equal to the original:
W=U I
Wz2;II
Vc)eUc
cUe)cV
II;2zW
I U=W
-
1\$\begingroup\$ Nice! I think I have a 6 and I'm trying to beat an uncooperative 4 into submission too, but they're both much more trivial than this. \$\endgroup\$Shaggy– Shaggy2018年07月16日 21:45:11 +00:00Commented Jul 16, 2018 at 21:45
-
\$\begingroup\$ @Shaggy I managed to get the same solution down to six after giving it a fresh look, four would be very impressive. \$\endgroup\$Etheryte– Etheryte2018年07月16日 23:02:40 +00:00Commented Jul 16, 2018 at 23:02
-
\$\begingroup\$ Given that input can (now) be a 1 dimensional array, this can be done with a score of 2. \$\endgroup\$Shaggy– Shaggy2018年07月17日 15:55:13 +00:00Commented Jul 17, 2018 at 15:55
-
\$\begingroup\$ @Shaggy I'm not sure if I see how, the source must be NxN. \$\endgroup\$Etheryte– Etheryte2018年07月17日 22:22:14 +00:00Commented Jul 17, 2018 at 22:22
-
Husk, size 3
Ṡ=↔
=↔=
↔=Ṡ
Try it online! Input as 1D-List. Each line defines a function, but only the one on the first line is called.
Ṡ=↔ is a palindrome tests which checks whether the input equals (=) its reverse (↔).
Husk, size 4
Ṡ=↔Σ
Σ↔=Ṡ
Ṡ=↔Σ
Σ↔=Ṡ
Try it online! For input as 2D-matrix, we concatenate (Σ) the list of lists before checking that it is a palindrome.
MATL, score 4
t,!P
]=%?
?%=]
P!,t
Input has the format [1 2; 2 1], using ; as row separator.
Output is via STDERR (allowed by default):
For centrosymmetric input a consistent error error is produced. With the current Linux and Octave versions in TIO the following STDERR output is produced (disregarding the final lines that start with
Real time: ...):octave: X11 DISPLAY environment variable not set octave: disabling GUI features Python 2.7.15 (default, May 16 2018, 17:50:09) [GCC 8.1.1 20180502 (Red Hat 8.1.1-1)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> >>> /usr/include/c++/8/bits/basic_string.h:1048: std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::reference std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::operator[](std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::reference = char&; std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]: Assertion '__pos <= size()' failed. panic: Aborted -- stopping myself...The error may be different depending on the Linux and Octave versions, but will be consistent across inputs.
For non-centrosymmetric input no error is produced, and the STDERR output on TIO is
octave: X11 DISPLAY environment variable not set octave: disabling GUI features Python 2.7.15 (default, May 16 2018, 17:50:09) [GCC 8.1.1 20180502 (Red Hat 8.1.1-1)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> >>>
Try it online! Or verify all test cases:
Check that the program is centrosymmetric.
Explanation
% is the comment symbol, which causes the rest of the line o be ignored. Newlines are also ignored. So the code is just
t,!P]=?P!,t
which does the following:
t % Input, implicit. Duplicate
, % Do twice
! % Transpose
P % Flip vertically
] % End
= % Compare element-wise
? % If all comparisons gave true
P % Implicit input. Transpose. Actually, since there is no more input, an
% error is prand the program exits. The rest of the code is not executed,
% and will be described in parentheses
! % (Transpose)
, % (Do twice)
t % (Duplicate)
% (End, implicit)
% (End, implicit)
Haskell, size (削除) 11 (削除ここまで), (削除) 10 (削除ここまで), (削除) 9 (削除ここまで), 8
Takes the input as a 2D list! (Credit to Ørjan Johansen)
r= --r}-
reverse
(==)<*>
r.map{-
-{pam.r
>*<)==(
esrever
-}r-- =r
-
\$\begingroup\$ Bugs: Second
<*>should be>*<. In the 2D one, there's a missing-and the reversed part doesn't have the spaces in the right places. \$\endgroup\$Ørjan Johansen– Ørjan Johansen2018年07月17日 23:58:00 +00:00Commented Jul 17, 2018 at 23:58 -
\$\begingroup\$ Got the 2D one down to 8 \$\endgroup\$Ørjan Johansen– Ørjan Johansen2018年07月18日 00:20:14 +00:00Commented Jul 18, 2018 at 0:20
Python 2, size 8
i=input#
a=i()###
a[::-1##
]!=a<_##
##_<a=!]
##1-::[a
###)(i=a
#tupni=i
Takes input as a 1D list of concatenated rows (of length \$n^2\$) and outputs via exit code (0 for symmetric inputs, 1 otherwise).
The list is simply checked to be a palindrome. If it is, the comparison chain a[::-1]!=a<_ fails at the != step and short-circuits; otherwise the unknown variable _ gets evaluated, crashing the program.
R, size 9
#)x-))
#(nacs-<x
#(ver(yna
# #
# #
# #
any(rev(#
x<-scan(#
))-x)#
The last three lines are the actual program that takes a 1D array as input and checks if it's equal to its reverse. Truthy value: FALSE, Falsy value: TRUE.
any(rev(x<-scan())-x)
R + pryr, size 9
pryr::f(#
any(rev(#
x)-x))#
#))x-)x
#(ver(yna
#(f::ryrp
C# (.NET Core), score (削除) 13 11 (削除ここまで) 10
l=>a=>a.//
Where((e//
,i)=>e!=//
a[l-i-1]//
).Any()///
///()ynA.)
//]1-i-l[a
//=!e>=)i,
//e((erehW
//.a>=a>=l
Takes input as 1D array and n2 as length; returns false for centrosymmetric and true for non-centrosymmetric. This version uses System.Linq but I don't know how to fit it in the code submission due to the particular requirements of the challenge. Unrolled code:
l => a => a.Where((e,i) => e != a[l-i-1]).Any()
Following is my previous submission that does not use LINQ:
C# (.NET Core), score (削除) 16 15 (削除ここまで) 13
l=>a=>{/////*
var r=1<2;for
(int i=0;i<l;
)r&=a[i]==a[l
-i++-1];
return r;}
/*/
};r nruter
;]1-++i-
l[a==]i[a=&r)
;l<i;0=i tni(
rof;2<1=r rav
*/////{>=a>=l
Unrolled code:
l => a =>
{
var r = 1<2;
for (int i = 0; i < l;)
r &= a[i] == a[l-i++-1];
return r;
}
-
\$\begingroup\$ If you change
inttovar(thus alsotnitorav), the second submission also works in JavaScript. \$\endgroup\$Adalynn– Adalynn2018年07月17日 17:48:10 +00:00Commented Jul 17, 2018 at 17:48
Ruby, score (削除) 9 (削除ここまで) 8
->z{z==#
z.######
reverse#
}#######
#######}
#esrever
######.z
#==z{z>-
A lambda taking a flattened matrix as input. Returns true for centrosymmetric, false otherwise.
-1 thanks to Mr.XCoder,
Unpacked:
->z{
z == z.reverse
}
Clean, size 9
Thanks to Ørjan Johansen!
import //
StdEnv//
r=reverse
?m=m==r//
m
//r==m=m?
esrever=r
//vnEdtS
// tropmi
Clean, size 10
import/*//
*/StdEnv//
r =reverse
?m //
=m==r m//
//m r==m=
// m?
esrever= r
//vnEdtS/*
//*/tropmi
-
1\$\begingroup\$ It's not because of newline, it works with indent. 9 lines \$\endgroup\$Ørjan Johansen– Ørjan Johansen2018年07月27日 06:43:07 +00:00Commented Jul 27, 2018 at 6:43
-
\$\begingroup\$ @ØrjanJohansen Can't believe I forgot you could indent them. Thanks! \$\endgroup\$Οurous– Οurous2018年07月27日 06:49:10 +00:00Commented Jul 27, 2018 at 6:49
-
\$\begingroup\$ You're welcome, and I edited the middle line after I posted, it just needs to be
m. \$\endgroup\$Ørjan Johansen– Ørjan Johansen2018年07月27日 06:54:30 +00:00Commented Jul 27, 2018 at 6:54 -
\$\begingroup\$ @ØrjanJohansen I've updated the answer to reflect that. It looks really elegant that way as well. \$\endgroup\$Οurous– Οurous2018年07月27日 07:05:33 +00:00Commented Jul 27, 2018 at 7:05
05AB1E, size 3 (11 bytes)
ÂQq
qQÂ
Input as a single list.
Try it online.
I guess this one doesn't count.. ;p
ÂQ
qâ
Input as a single list.
Try it online.
Explanation:
 # Bifurcate this list (short for DR: Duplicate & Reverse copy)
Q # Check if the input-list and the reversed list are equal
q # Terminate the program
qQâ # No-ops
Some size 3 (11 bytes) alternatives:
R€R
QqQ
R€R
Matrix as input.
Try it online.
RQ
q
QR
Single list as input.
Try it online.
C (gcc), 12 x 12
C(e,n,t,r)//
int*e;{for//
(r=t=0;e[t//
];r|=e[t]^//
e[~t+++n])//
;n=!!!!r;}//
//};r!!!!=n;
//)]n+++t~[e
//^]t[e=|r;]
//t[e;0=t=r(
//rof{;e*tni
//)r,t,n,e(C
><>, Size 6
!/l2(\
s\?\ ?
/{=/ ;
: /={/
? \?\s
\(2l/!
(It turns out it's a pain passing multiple value to -v in tio)
Input is taken as a one dimensional array as the initial stack state with -v. Exits with no output if the matrix is centrosymmetric, exits with an error (prints "Something smells fishy...") if not.
l2( Checks if the length of the stack is less than 2
?; Exits cleanly if so
{= Checks if the first and last elements are equal
?\s Causes an error ('s' is not a valid instruction) if not
I wasn't entirely happy with this output format, so here's a size 7 one which outputs 1 for true, and 0 for false.
!/l2(\\
? ?0
=;n1/n
;{ {;
n/1n;=
0? ?
\\(2l/!
-
\$\begingroup\$ Nice job! ><> can be a fun language to work with, unless it's a complex program, then it's a pain. \$\endgroup\$mbomb007– mbomb0072018年07月17日 21:40:45 +00:00Commented Jul 17, 2018 at 21:40
-
1\$\begingroup\$ if you swap the truthy and falsey values you can get it down to 4 with
{-?;\$\endgroup\$Jo King– Jo King2018年07月17日 22:52:38 +00:00Commented Jul 17, 2018 at 22:52
Stax, n = 3
$cr
=q=
rc$
Explanation:
$cr =q= rc$ Full program, implicit input
$ Flatten
cr Copy and reverse
= Compare to original
q Print result
= Silently error because there is only one item on stack
rc$ Not executed
3 is the best possible because I need at least three commands: Copy, reverse, and compare
Stax, n = 4
Mrxr
M=qX
Xq=M
rxrM
Explanation:
Mrxr M=qX Xq=M rxrM Full program, implicit input
Mr Transpose and reverse
x Push input again
r M Reverse and transpose
= Compare
qX Xq Print result twice and save to X
= Silently error because there is only one item on stack
M rxrM Not executed
Java 10, size 13 (181 bytes)
a->{var r///*
=1>0;for(int
l=a.length,i=
0;i<l;)r&=a[i
]==a[l-++i];
return r;}
/*/
};r nruter
;]i++-l[a==]
i[a=&r);l<i;0
=i,htgnel.a=l
tni(rof;0>1=
*///r rav{>-a
Inspired by @Charlie's C# answer.
Golfed explanation:
a->{var r=1>0;for(int l=a.length,i=0;i<l;)r&=a[i]==a[l-++i];return r;}
a->{ // Method with byte-array parameter and boolean return-type
var r=1>0; // Result-boolean, starting at true
for(int l=a.length, // The length of the input-array
i=0;i<l;) // Loop `i` in the range [0, length)
r&= // Bitwise-AND the result-boolean with:
a[i] // Whether the current item
==a[l-++i]; // equals the opposite item
return r;} // Return the result
C (gcc), score 11
Takes a list of int and n as arguments. Returns n as truthy value, 0 as falsy.
/*//};n=l;k
--*==++l*=*
n);l>k;n*n+
l=k(rof{;l*
tni)n,l(f;k
*/*/////*/*
k;f(l,n)int
*l;{for(k=l
+n*n;k>l;)n
*=*l++==*--
k;l=n;}//*/
Javascript ES6, size 8:
/***/a=>
""+a==a.
reverse(
)/////*/
/*/////)
(esrever
.a==a+""
>=a/***/
Javascript ES6, size 7 (is it valid?):
a=>""+a
==a.//=
reverse
` `
esrever
=//.a==
a+"">=a
Test:
<script>
f=
a=>""+a
==a.//=
reverse
` `
esrever
=//.a==
a+"">=a
</script>
<script>
console.log([
[1, 2, 2, 1],
[1, 2, 3, 5, 6, 5, 3, 2, 1],
[10, 5, 30, 2, 6, 2, 30, 5, 10],
[100, 100, 100, 100, 50, 100, 100, 100, 100],
[1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 1],
[3, 4, 5, 6, 7, 5, 6, 7, 8, 9, 3, 2, 10, 2, 3, 9, 8, 7, 6, 5, 7, 6, 5, 4, 3],
].every(f))
console.log([
[1, 2, 1, 2],
[1, 2, 10, 5, 6, 5, 11, 2, 1],
[14, 5, 32, 2, 6, 2, 30, 5, 16],
[19, 19, 19, 40, 50, 4, 19, 19, 19],
[1, 2, 20, 4, 7, 6, 7, 8, 8, 7, 6, 6, 3, 3, 2, 1],
[3, 4, 5, 6, 7, 5, 6, 7, 8, 9, 4, 5, 10, 4, 5, 5, 6, 7, 8, 9, 3, 4, 5, 6, 7],
].every(x=>!f(x)))
</script>
or with lambda saved to variable named a:
a=
a=>""+a
==a.//=
reverse
` `
esrever
=//.a==
a+"">=a
console.log([
[1, 2, 2, 1],
[1, 2, 3, 5, 6, 5, 3, 2, 1],
[10, 5, 30, 2, 6, 2, 30, 5, 10],
[100, 100, 100, 100, 50, 100, 100, 100, 100],
[1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 1],
[3, 4, 5, 6, 7, 5, 6, 7, 8, 9, 3, 2, 10, 2, 3, 9, 8, 7, 6, 5, 7, 6, 5, 4, 3],
].every(a))
console.log([
[1, 2, 1, 2],
[1, 2, 10, 5, 6, 5, 11, 2, 1],
[14, 5, 32, 2, 6, 2, 30, 5, 16],
[19, 19, 19, 40, 50, 4, 19, 19, 19],
[1, 2, 20, 4, 7, 6, 7, 8, 8, 7, 6, 6, 3, 3, 2, 1],
[3, 4, 5, 6, 7, 5, 6, 7, 8, 9, 4, 5, 10, 4, 5, 5, 6, 7, 8, 9, 3, 4, 5, 6, 7],
].every(x=>!a(x)))
Clojure, Size 9
#(= %(;;;
reverse;;
(map;;;;;
reverse;;
%)));)))%
;;esrever
;;;;;pam(
;;esrever
;;;(% =(#
Explore related questions
See similar questions with these tags.
#), so that the bottom half of the code would all be a comment. \$\endgroup\$#won't work because comments preceded by#are inline only :P \$\endgroup\$