33
\$\begingroup\$

I once had a beautiful rectangular array. It was very symmetrical, but unfortunately it has fallen apart and now I only have the top left corner. Your task will be to rebuild the original array.

Your program will receive a 2 dimensional array of integers. For ease of parsing, you may assume they are all between 1 and 9. Your task is to reverse the array's columns, its rows, and both, stitch together the resulting corners, and return the resulting array.

You can assume the array dimensions will be at least 1x1.

Test cases:

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

This is , fewest bytes wins!

Mr. Xcoder
42.8k9 gold badges87 silver badges221 bronze badges
asked Mar 5, 2018 at 16:00
\$\endgroup\$
9
  • 1
    \$\begingroup\$ I'll bet charcoal can do this in under 10 \$\endgroup\$ Commented Mar 5, 2018 at 16:19
  • 1
    \$\begingroup\$ @tbfninja chat.stackexchange.com/transcript/message/43184083#43184083 but could maybe be shorter with a different input format. \$\endgroup\$ Commented Mar 5, 2018 at 16:20
  • \$\begingroup\$ @MagicOctopusUrn yes \$\endgroup\$ Commented Mar 5, 2018 at 16:39
  • 2
    \$\begingroup\$ @tfbninja WS⟦ι⟧‖M→↓ perhaps? 5 bytes to read the input and 4 to reflect it. \$\endgroup\$ Commented Mar 5, 2018 at 17:51
  • 4
    \$\begingroup\$ I'm 99% sure that there is a lang that do this with (or some similar character) just can't remember which one :c \$\endgroup\$ Commented Mar 5, 2018 at 19:36

37 Answers 37

1
2
19
\$\begingroup\$

Canvas, 1 byte

Try it here!

Outputs as a multiline string

answered Mar 5, 2018 at 20:45
\$\endgroup\$
3
  • 2
    \$\begingroup\$ Nice, @Rod called that one hah! Is this your language too Dzaima? \$\endgroup\$ Commented Mar 5, 2018 at 20:49
  • \$\begingroup\$ Damn... I should've remembered... \$\endgroup\$ Commented Mar 5, 2018 at 20:53
  • \$\begingroup\$ @MagicOctopusUrn Yeah, IIRC it's meant to basically be S.O.G.L. II: Electric Boogaloo? \$\endgroup\$ Commented Mar 6, 2018 at 10:50
13
\$\begingroup\$

Haskell, (削除) 25 (削除ここまで) 24 bytes

r=(++)<*>reverse
r.map r

Try it online!

answered Mar 5, 2018 at 16:24
\$\endgroup\$
5
\$\begingroup\$

Python 3, 38 bytes

lambda a:[b+b[::-1]for b in a+a[::-1]]

Try it online!

Takes a list of lists and returns a list of lists.

Explanation:

lambda a: # anonymous lambda function
 for b in a+a[::-1] # for each row in the array and the upside-down array
 b+b[::-1] # the row with its reverse appended
 [ ] # return in a list
answered Mar 5, 2018 at 16:07
\$\endgroup\$
5
\$\begingroup\$

Husk, (削除) 7 (削除ここまで) 6 bytes

Coincidentally, Erik had posted the exact same code in the Husk chatroom about a minute before I posted this.

!!oTS+↔

Try it online!

Pervious version, 7 bytes:

mS+↔S+↔
answered Mar 5, 2018 at 16:19
\$\endgroup\$
5
\$\begingroup\$

Retina, 13 bytes

\%`$
$^$`
Vs`

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.

Vs`

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 option s, 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.

answered Mar 5, 2018 at 16:34
\$\endgroup\$
3
  • \$\begingroup\$ This doesn't look like any regex flavor I know of :P \$\endgroup\$ Commented Mar 5, 2018 at 16:36
  • \$\begingroup\$ @Pavel Because Retina isn't just regex. :) \$\endgroup\$ Commented Mar 5, 2018 at 16:36
  • \$\begingroup\$ @Pavel the only part of that code that is an actual regex is the $ on the first line. ;) I'll add an explanation later. \$\endgroup\$ Commented Mar 5, 2018 at 17:01
5
\$\begingroup\$

05AB1E, 2 bytes

∞∊

Try it online!


 # Input:Array of String | ['12','34']
---#-----------------------+------------------------------------------
∞ # Mirror horizontally. | [12,34] -> [1221,3443]
 ∊ # Mirror vertically. | [1221,3443] -> [1221\n3443\n3443\n1221]

Credit for Mr. Xcoder pointing out that arrays of string may count as 2D arrays and Pavel for confirming it.

FryAmTheEggman
17.5k3 gold badges42 silver badges99 bronze badges
answered Mar 5, 2018 at 16:23
\$\endgroup\$
5
  • \$\begingroup\$ ... 2 bytes \$\endgroup\$ Commented Mar 5, 2018 at 16:35
  • \$\begingroup\$ For ease of parsing, you may assume they are all between 1 and 9 – So I think this is valid. Awaiting Pavel's confirmation, I guess \$\endgroup\$ Commented Mar 5, 2018 at 16:37
  • \$\begingroup\$ @Mr.Xcoder that was what I had initially, then the TIO 2D arrays as input was weird... so had to come up with that header stuff. \$\endgroup\$ Commented Mar 5, 2018 at 16:38
  • \$\begingroup\$ A string is an array of characters, so a list of strings is still a 2d array. @Mr.Xcoder's solution is valid. \$\endgroup\$ Commented Mar 5, 2018 at 16:38
  • \$\begingroup\$ Coolio, works for me. \$\endgroup\$ Commented Mar 5, 2018 at 16:41
4
\$\begingroup\$

MATL, 5 bytes

,tPv!

Try it online!

Explanation:

(implicit input)
, # do twice:
 t # dup top of stack
 P # flip vertically
 v # vertically concatenate
 ! # transpose
(implicit output)

answered Mar 5, 2018 at 17:01
\$\endgroup\$
3
\$\begingroup\$

Jelly, 5 bytes

m0ドルm0

Try it online!

answered Mar 5, 2018 at 16:02
\$\endgroup\$
1
  • \$\begingroup\$ Also 5 bytes: m0Z$+ (by Hyper Neutrino). \$\endgroup\$ Commented Mar 5, 2018 at 16:05
3
\$\begingroup\$

Japt, 6 bytes

mê1 ê1

Try it here


Explanation

 :Implicit input of 2D array
m :Map
 ê1 : Mirror sub array
 ê1 :Mirror main array
answered Mar 5, 2018 at 17:41
\$\endgroup\$
3
\$\begingroup\$

Octave, (削除) 33 (削除ここまで) 29 bytes

Thanks to @Giuseppe for golfing four bytes!

@(A)[B=[A;flip(A)] fliplr(B)]

Try it online!

answered Mar 5, 2018 at 20:46
\$\endgroup\$
1
  • \$\begingroup\$ 29 bytes \$\endgroup\$ Commented Mar 5, 2018 at 21:12
3
\$\begingroup\$

Ruby, 35 bytes

->a{r=->b{b+b.reverse}
r[a].map &r}

Try it online!

A lambda accepting a 2D array and returning a 2D array. It's straightforward, but here's the ungolfed version anyway:

->a{
 r=->b{ b+b.reverse } # r is a lambda that returns the argument and its reverse
 r[a].map &r # Add the array's reverse, then add each row's reverse
}
answered Mar 5, 2018 at 22:46
\$\endgroup\$
3
\$\begingroup\$

JavaScript (Node.js), (削除) 62 (削除ここまで) (削除) 55 (削除ここまで) (削除) 49 (削除ここまで) 46 bytes

A=>(j=x=>[...x,...[...x].reverse()])(A).map(j)

Try 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.

answered Mar 6, 2018 at 3:54
\$\endgroup\$
3
\$\begingroup\$

J, 12 bytes

(,|.)@,.|."1

Try it online!

Explanation

 |."1 - reverse each row
 ,. - and stitch them to the input
 ( )@ - and 
 ,|. - append the rows in reversed order 
answered Mar 6, 2018 at 8:35
\$\endgroup\$
3
\$\begingroup\$

awk, 88 bytes

{s="";for(i=NF;i>0;i--)s=" "$i s" "$i;a[FNR]=s;print s}END{for(i=NR;i>0;i--)print a[i]}
answered Mar 8, 2018 at 15:34
\$\endgroup\$
1
  • 3
    \$\begingroup\$ Welcome to PPCG! Nice first answer :) \$\endgroup\$ Commented Mar 8, 2018 at 15:34
3
\$\begingroup\$

Uiua, 9 bytes

⍥(⍉⊂∶⇌.)2

Try it!

⍥(⍉⊂∶⇌.)2
⍥( )2 # run a function twice
 . # duplicate
 ⇌ # reverse
 ⊂∶ # prepend
 ⍉ # transpose
answered Nov 13, 2023 at 19:26
\$\endgroup\$
2
\$\begingroup\$

Triangularity, 31 bytes

...)...
..IEM..
.DRs+}.
DRs+...

Try it online!

Explanation

Removing the characters that make up for the padding, here is what the program does:

)IEMDRs+}DRs+ – Full program. Takes a matrix as a 2D list from STDIN.
) – Push a 0 onto the stack.
 I – Take the input at that index.
 E – Evaluate it.
 M } – For each row...
 DR – Duplicate and replace the second copy by its reverse.
 s+ – Swap and append.
 DR – Duplicate the result and replace the second copy by its reverse.
 s+ – Swap and append.
answered Mar 5, 2018 at 16:11
\$\endgroup\$
2
\$\begingroup\$

R, 57 bytes

function(m)rbind(N<-cbind(m,m[,ncol(m):1]),N[nrow(N):1,])

Try it online!

answered Mar 5, 2018 at 16:40
\$\endgroup\$
2
\$\begingroup\$

APL+WIN, 11 bytes

Prompts for a 2d array of integers.

m⍪⊖m←m,⌽m←⎕
answered Mar 5, 2018 at 16:46
\$\endgroup\$
2
\$\begingroup\$

Stax, 5 bytes

:mm:m

Run and debug it online

:m means mirror, which is input.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.

answered Mar 5, 2018 at 17:02
\$\endgroup\$
0
2
\$\begingroup\$

Mathematica, 29 bytes

(g=#~Join~Reverse@#&)@*Map[g]

Try it online!

answered Mar 5, 2018 at 18:55
\$\endgroup\$
2
\$\begingroup\$

SOGL V0.12, 2 bytes

-1 byte thanks to dzaima.

╬ø

Try it here!

answered Mar 5, 2018 at 20:20
\$\endgroup\$
0
2
\$\begingroup\$

APL (Dyalog Classic), 7 bytes

⍪∘⊖⍨⊢,⌽

Try it online!

answered Mar 5, 2018 at 21:21
\$\endgroup\$
2
\$\begingroup\$

Java 8, (削除) 140 (削除ここまで) 131 bytes

m->{String r="";for(int a=m.length,b=m[0].length,i=a+a,j;i-->0;r+="\n")for(j=b+b;j-->0;)r+=m[i<a?i:a+a+~i][j<b?j:b+b+~j];return r;}

Explanation:

Try it online.

m->{ // Method with integer-matrix parameter and String return-type
 String r=""; // Result-String, starting empty
 for(int a=m.length, // Amount of rows of the input-matrix
 b=m[0].length, // Amount of columns of the input-matrix
 i=a+a,j; // Index integers
 i-->0; // Loop over double the rows
 r+="\n") // After every iteration: append a new-line to the result
 for(j=b+b;j-->0;) // Inner loop over double the columns
 r+= // Append the result with:
 m[i<a? // If `i` is smaller than the amount of rows
 i // Use `i` as index in the input-matrix
 : // Else:
 a+a+~i] // Use `a+a+i-1` as index instead
 [j<b? // If `j` is smaller than the amount of columns
 j // Use `j` as index in the input-matrix
 : // Else:
 b+b+~j]; // Use `b+b+j-1` as index instead
 return r;} // Return the result-String
answered Mar 6, 2018 at 8:16
\$\endgroup\$
2
\$\begingroup\$

J, 11 bytes

Anonymous tacit prefix function.

|:@(,|.)^:2

Try it online!

|: transpose

@(...) the result of:

, the argument followed by

|. its reverse

^:2 and all this done twice

answered Mar 6, 2018 at 10:36
\$\endgroup\$
2
\$\begingroup\$

SNOBOL4 (CSNOBOL4), (削除) 119 (削除ここまで) 113 bytes

	T =TABLE()
I	X =X + 1
	I =INPUT	:F(D)
	OUTPUT =T<X> =I REVERSE(I)	:(I)
D	X =X - 1
	OUTPUT =GT(X) T<X>	:S(D)
END	

Try it online!

Takes input as strings on STDIN, without spaces. This only works because the digits are 1-9 and would fail otherwise.

answered Mar 5, 2018 at 17:11
\$\endgroup\$
2
  • \$\begingroup\$ I can see why people don't use this language anymore. This is so weird. \$\endgroup\$ Commented Mar 5, 2018 at 17:15
  • 1
    \$\begingroup\$ @Pavel SNOBOL is truly a terrible language to work with. this is a more modern C implementation of it which has additional builtin functions like REVERSE; the original only supported integer arithmetic as well, as far as I can tell. \$\endgroup\$ Commented Mar 5, 2018 at 17:29
2
\$\begingroup\$

C (gcc), (削除) 114 (削除ここまで) 111 bytes

j,i;f(A,w,h)int*A;{for(i=h+h;i-->0;puts(""))for(j=w+w;j-->0;)printf("%d,",A[(i<h?i:h+h+~i)*w+(j<w?j:w+w+~j)]);}

Try it online!

C (gcc), 109 bytes (abusing ease of parsing)

  • Thanks to Kevin Cruijssen for suggesting to only allow one-digit input integers; saved two bytes.
j,i;f(A,w,h)int*A;{for(i=h+h;i-->0;puts(""))for(j=w+w;j-->0;)putchar(A[(i<h?i:h+h+~i)*w+(j<w?j:w+w+~j)]+48);}

Try it online!

answered Mar 6, 2018 at 0:13
\$\endgroup\$
5
  • \$\begingroup\$ You can save 3 bytes by inverting the loops. for(i=h+h;i-->0;puts(""))for(j=w+w;j-->0;) \$\endgroup\$ Commented Mar 6, 2018 at 8:18
  • \$\begingroup\$ Does not fulfill spec; prints the array, rather than returning it. \$\endgroup\$ Commented Mar 6, 2018 at 11:14
  • \$\begingroup\$ "For ease of parsing, you may assume they are all between 1 and 9.", so you can remove the comma in the printf("%d" for an additional -1 byte. \$\endgroup\$ Commented Mar 6, 2018 at 15:12
  • \$\begingroup\$ @Rogem I would say printing the array falls under accepted i/o. \$\endgroup\$ Commented Mar 6, 2018 at 19:07
  • 1
    \$\begingroup\$ @KevinCruijssen Thanks a lot; using the ease of parsing I managed to shave off another byte. \$\endgroup\$ Commented Mar 6, 2018 at 19:12
2
\$\begingroup\$

Charcoal, 5 bytes

θ‖C→↓

Try it online!

Thanks to ASCII-only for a better input format.

answered Mar 5, 2018 at 18:40
\$\endgroup\$
9
  • \$\begingroup\$ I wonder if this input format is valid, since I'm afraid Charcoal can't handle input otherwise. If it isn't, I'll happily delete this answer. \$\endgroup\$ Commented Mar 5, 2018 at 18:41
  • \$\begingroup\$ This is valid I/o. \$\endgroup\$ Commented Mar 5, 2018 at 18:42
  • \$\begingroup\$ @Pavel I just wondered because you had said that "Your program will receive a 2 dimensional array of integers", while a string is 1-dimensional (and no, the outer [] don't exactly make it 2D). \$\endgroup\$ Commented Mar 5, 2018 at 18:44
  • \$\begingroup\$ @ASCII-only Charcoal really needs a better I/O method... \$\endgroup\$ Commented Mar 5, 2018 at 20:09
  • \$\begingroup\$ @Neil He didn't get pinged here, but I pinged him over TNB. :) \$\endgroup\$ Commented Mar 5, 2018 at 20:13
2
\$\begingroup\$

Clojure, 56 bytes

(fn[i](map #(concat %(reverse %))(concat i(reverse i))))

Creates an anonymous function which takes the two dimensional list and returns the fixed rectangle as two dimensional list.

Try it online!

answered Mar 8, 2018 at 14:11
\$\endgroup\$
2
\$\begingroup\$

Add++, 30 bytes

D,f,@,bU€{r}B]{r}
D,r,@,dbR+

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.

answered Mar 5, 2018 at 17:15
\$\endgroup\$
2
\$\begingroup\$

Proton, 29 bytes

a=>[b+b[by-1]for b:a+a[by-1]]

Try it online!

There are a few other interesting approaches though:

Proton, 29 bytes

a=>map(g,(g=x=>x+x[by-1])(a))

Try it online!

You can define the mirror sub-function g in-line, because Proton. It's not shorter though.

Proton, 36 bytes

(a=>[x[0]for x:zip(*(a+a[by-1]))])*2

Try it 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).

answered Apr 2, 2019 at 16:31
\$\endgroup\$
1
2

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.