Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Is the matrix centrosymmetric... and so is the code?

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

Answer*

Draft saved
Draft discarded
Cancel
4
  • 1
    \$\begingroup\$ It's not because of newline, it works with indent. 9 lines \$\endgroup\$ Commented Jul 27, 2018 at 6:43
  • \$\begingroup\$ @ØrjanJohansen Can't believe I forgot you could indent them. Thanks! \$\endgroup\$ Commented 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\$ Commented 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\$ Commented Jul 27, 2018 at 7:05

AltStyle によって変換されたページ (->オリジナル) /