30
\$\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\$
  • 1
    \$\begingroup\$ I'll bet charcoal can do this in under 10 \$\endgroup\$ – FantaC Mar 5 '18 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 Mar 5 '18 at 16:20
  • \$\begingroup\$ @MagicOctopusUrn yes \$\endgroup\$ – Pavel Mar 5 '18 at 16:39
  • 2
    \$\begingroup\$ @tfbninja WS⟦ι⟧‖M→↓ perhaps? 5 bytes to read the input and 4 to reflect it. \$\endgroup\$ – Neil Mar 5 '18 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 Mar 5 '18 at 19:36

33 Answers 33

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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