Definición
A centrosymmetric matrix is a square matrix that is symmetric about its center. More rigorously, a matrix of size is centrosymmetric if, for any the following relation is satisfied:
Examples of such matrices
Here is an illustration of the symmetry of a matrices like these (borrowed from the aforementioned Wikipedia article):
An even-side-length () centrosymmetric matrix:
And an odd-side-length () one:
Task and Specs
Given a square matrix of size at least , 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 lines, each of which containing 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 , with a lower 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, , as input too (unless you take input as a 1D list, in which case you may only take 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]]
#
), so that the bottom half of the code would all be a comment.#
won't work because comments preceded by#
are inline only :PRespuestas:
JavaScript (ES6), size
12119All 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.Try it online!
Matrix + width, size 11 (131 bytes)
Takes input in currying syntax
(width)(matrix)
.Try it online!
Payload
Container
Matrix only, size 12 (155 bytes)
This is my original solution, which computes the width of the matrix by itself.
Try it online!
How?
We need a few critical tokens that cannot be split:
some
length
)=>
Line-feeds may be inserted almost anywhere else.
The unrolled payload code reads as:
and is wrapped inside the following structure:
which, once reversed, becomes a valid block comment:
fuente
Jelly, score 2
Try it online!
Takes input as a flattened square matrix (vector of sizen2 ).
fuente
Befunge-93, size 24
Try it online!
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:
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:
This sections checks every indexi,j [1-indexed] against the condition Ai,j=An+1−i,n+1−j . However, we have a problem: we have j offset by 8 . To fix that, the following formulae are used:
The other portions of code are unread garbage to make it centrosymmetric.
fuente
Haskell,n=8
No comments!
Takes input as a 1-D list
Try it online!
Takes input as a 2-D matrix
Try it online!
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: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.
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 haveIn order to trim down our line length we alias
concat
andreverse
.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.fuente
<$>
was also<$>
.Python 2, size 10 (109 bytes)
Try it online!
Previous Python 2, size 14 (209 bytes)
Try it online!
fuente
-i-1
and-j-1
could simply be~i
and~j
Pyth, size 6 (41 bytes)
Try it here
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 theq
before it, is a no-op.fuente
APL (Dyalog Unicode), score
7654Full program. Prompts for matrix expression from stdin. Prints a 1 or a 0 and then throws a consistent error. No comments!
Try it online!
⎕
prompt for expression and evaluate it,
ravel (flatten) itr←
store the result inr
(for ravelled)⌽r
mirrorr
r≡
implicitly print whetherr
is identical to that≡
depth ofr
(gives 1)r⌽
user
to rotate that(this will always cause a
RANK ERROR
and quit because a matrix is not permitted as left argument to⌽
)fuente
Perl 6, size
9 87Try it online!
Okay, I've changed it toreverse
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...
:(
Though if we take it as a one dimensional array, it's easy to fit it in a size 6.
Try it online!
fuente
Wolfram Language (Mathematica) (REPL only), size 8 (71 bytes)
Returns
True
for centrosymmetric input, andFalse
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.fuente
Pyt, size 1
Try it online!
Checks if the input (in the form of concatenated rows) is a palindrome.
fuente
Japt, size
94 (11 bytes)Try it online!
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:
Try it online!
fuente
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.fuente
MATL, score 4
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: ...
):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
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 justwhich does the following:
fuente
Haskell, size
11,10,9, 8Takes the input as a 2D list! (Credit to Ørjan Johansen)
fuente
<*>
should be>*<
. In the 2D one, there's a missing-
and the reversed part doesn't have the spaces in the right places.Python 2, size 8
Try it online!
Takes input as a 1D list of concatenated rows (of lengthn2 ) 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.fuente
R, size 9
Try it online!
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.
R + pryr, size 9
Try it online!
fuente
C# (.NET Core), score
13 1110Try it online!
Takes input as 1D array and n2 as length; returns
false
for centrosymmetric andtrue
for non-centrosymmetric. This version usesSystem.Linq
but I don't know how to fit it in the code submission due to the particular requirements of the challenge. Unrolled code:Following is my previous submission that does not use LINQ:
C# (.NET Core), score
16 1513Try it online!
Unrolled code:
fuente
int
tovar
(thus alsotni
torav
), the second submission also works in JavaScript.Ruby, score
98Try it online!
A lambda taking a flattened matrix as input. Returns true for centrosymmetric, false otherwise.
-1 thanks to Mr.XCoder,
Unpacked:
fuente
Clean, size 9
Thanks to Ørjan Johansen!
Try it online!
Clean, size 10
Try it online!
fuente
m
.05AB1E, size 3 (11 bytes)
Input as a single list.
Try it online.
I guess this one doesn't count.. ;p
Input as a single list.
Try it online.
Explanation:
Some size 3 (11 bytes) alternatives:
Matrix as input.
Try it online.
Single list as input.
Try it online.
fuente
C (gcc), 12 x 12
Try it online!
fuente
><>, Size 6
Try it online!
(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.
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.
Try it online!
fuente
{-?;
Stax, n = 3
Run and debug it
Explanation:
3 is the best possible because I need at least three commands: Copy, reverse, and compare
Stax, n = 4
Run and debug it
Explanation:
fuente
Japt, size 2 (5 bytes)
Try it
fuente
Java 10, size 13 (181 bytes)
Try it online.
Inspired by @Charlie's C# answer.
Golfed explanation:
fuente
C (gcc), score 11
Takes a list of int and n as arguments. Returns n as truthy value, 0 as falsy.
Try it online!
fuente
Javascript ES6, size 8:
Javascript ES6, size 7 (is it valid?):
Test:
or with lambda saved to variable named
a
:fuente
Clojure, Size 9
Try it online!
fuente