32
\$\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!

\$\endgroup\$
9
  • 1
    \$\begingroup\$ I'll bet charcoal can do this in under 10 \$\endgroup\$
    – qqq
    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\$
    – Pavel
    Commented Mar 5, 2018 at 16:20
  • \$\begingroup\$ @MagicOctopusUrn yes \$\endgroup\$
    – Pavel
    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\$
    – Neil
    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\$
    – Rod
    Commented Mar 5, 2018 at 19:36

34 Answers 34

1
2
1
\$\begingroup\$

Python 3, 49 bytes

def f(a):o=[i+i[::-1]for i in a];return o+o[::-1]

Where a is the input array. This function will return a 2D array.

\$\endgroup\$
1
\$\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!

\$\endgroup\$
1
\$\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).

\$\endgroup\$
1
\$\begingroup\$

Perl 5 -lp -Mfeature=say, 36 bytes

unshift@l,$_.=reverse;END{say for@l}

Deparsed:

BEGIN { $/ = "\n"; $\ = "\n"; }
use feature 'say';
LINE: while (defined($_ = readline ARGV)) {
    chomp $_;
    unshift @l, $_ .= reverse;
    sub END {
        say $_ foreach (@l);
    }
}
continue {
    die "-p destination: $!\n" unless print $_;
}

Try it online!

\$\endgroup\$
1
2

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.