Certa vez, tive uma bela matriz retangular. Era muito simétrico, mas infelizmente desmoronou e agora só tenho o canto superior esquerdo. Sua tarefa será reconstruir a matriz original.
Seu programa receberá uma matriz bidimensional de números inteiros. Para facilitar a análise, você pode assumir que todos estão entre 1 e 9. Sua tarefa é inverter as colunas da matriz, suas linhas e ambas, costurar os cantos resultantes e retornar a matriz resultante.
Você pode assumir que as dimensões da matriz serão pelo menos 1x1.
Casos de teste:
Input:
1 2 3
4 5 6
Output:
1 2 3 3 2 1
4 5 6 6 5 4
4 5 6 6 5 4
1 2 3 3 2 1
Input:
1
Output:
1 1
1 1
Input:
9
9
9
Output:
9 9
9 9
9 9
9 9
9 9
9 9
Isso é código-golfe , o menor número de bytes vence!
WS⟦ι⟧‖M→↓
talvez? 5 bytes para ler a entrada e 4 para refleti-la.╬
(ou algum personagem similar) só não lembro qual: cRespostas:
Próton , 29 bytes
Experimente online!
Existem algumas outras abordagens interessantes:
Próton , 29 bytes
Experimente online!
Você pode definir a subfunção de espelho
g
in-line, porque Proton. Não é mais curto.Próton , 36 bytes
Experimente online!
This should be
(a=>zip(*(a+a[by-1])))*2
which is 24 bytes, but the zip function is completely broken. Basically, you mirror it and zip, and then do that twice (you can multiply a function by a positive integer to apply the function multiple times).fonte
Canvas, 1 byte
Try it here!
Outputs as a multiline string
fonte
Haskell,
2524 bytesTry it online!
fonte
Python 3, 38 bytes
Try it online!
Takes a list of lists and returns a list of lists.
Explanation:
fonte
Husk,
76 bytesCoincidentally, Erik had posted the exact same code in the Husk chatroom about a minute before I posted this.
Try it online!
Pervious version, 7 bytes:
fonte
Retina, 13 bytes
Try it online!
Explanation
On each line (
%
), match the end of the line ($
), and insert the reverse ($^
) of the entire line ($`
) and print the result with a trailing linefeed (\
). This does the reflection along the vertical axis and prints the first half of the output.This just reverses the entire string, which is equivalent to a 180° degree rotation, or in our case (due to the horizontal symmetry) a reflection along the horizontal axis. This way this works is that
V
's (reverse) default regex is(?m:^.*$)
, which normally matches each line of the string. However, we activate the singleline options
, which makes.
match linefeeds as well and therefore this default regex actually matches the entire string.The result of this is printed automatically at the end of the program, giving us the second half of the output.
fonte
$
on the first line. ;) I'll add an explanation later.05AB1E, 2 bytes
Try it online!
Credit for Mr. Xcoder pointing out that arrays of string may count as 2D arrays and Pavel for confirming it.
fonte
Jelly, 5 bytes
Try it online!
fonte
m0Z$⁺
(by Hyper Neutrino).MATL, 5 bytes
Try it online!
Explanation:
fonte
Octave,
3329 bytesThanks to @Giuseppe for golfing four bytes!
Try it online!
fonte
JavaScript (Node.js),
62554946 bytesTry it online!
Because
Array.prototype.reverse()
reverses the array in place, I have to make a shallow copy somewhere first.A=>(j=x=>[...x,...x.reverse()])(A).map(j)
does not work.fonte
J, 12 bytes
Try it online!
Explanation
fonte
awk, 88 bytes
fonte
Triangularity, 31 bytes
Try it online!
Explanation
Removing the characters that make up for the padding, here is what the program does:
fonte
R, 57 bytes
Try it online!
fonte
APL+WIN, 11 bytes
Prompts for a 2d array of integers.
fonte
Stax, 5 bytes
Run and debug it online
:m
means mirror, which isinput.concat(reverse(input))
.m
, in this context means output each line after applying...So, mirror the array of rows, and then mirror each row and output.
fonte
Japt, 6 bytes
Try it here
Explanation
fonte
Mathematica, 29 bytes
Try it online!
fonte
SOGL V0.12, 2 bytes
-1 byte thanks to dzaima.
Try it here!
fonte
APL (Dyalog Classic), 7 bytes
Try it online!
fonte
Ruby, 35 bytes
Try it online!
A lambda accepting a 2D array and returning a 2D array. It's straightforward, but here's the ungolfed version anyway:
fonte
Java 8,
140131 bytesExplanation:
Try it online.
fonte
J, 11 bytes
Anonymous tacit prefix function.
Try it online!
|:
transpose@(…)
the result of:,
the argument followed by|.
its reverse^:2
and all this done twicefonte
SNOBOL4 (CSNOBOL4),
119113 bytesTry it online!
Takes input as strings on STDIN, without spaces. This only works because the digits are
1-9
and would fail otherwise.fonte
REVERSE
; the original only supported integer arithmetic as well, as far as I can tell.C (gcc),
114111 bytesTry it online!
C (gcc), 109 bytes (abusing ease of parsing)
Try it online!
fonte
for(i=h+h;i-->0;puts(""))for(j=w+w;j-->0;)
printf("%d"
for an additional -1 byte.Charcoal, 5 bytes
Try it online!
Thanks to ASCII-only for a better input format.
fonte
[]
don't exactly make it 2D).Add++, 30 bytes
Try it online!
The footer simply transforms the nested array into the format in the question. Defines a function
f
, which expects a matrix (nested array) as an argument.fonte
Julia 0.6,
5549 bytesTry it online!
~(i)
is a function to create slice fromi
down to1
.So
~end
gives the sliceend:-1:1
!(x)
is the function to do the rebuilding of the array.fonte
V, 12 bytes
Try it online!
Explanation:
fonte