Although it's done a few times as sub-challenge of a larger challenge, and we also have a challenge to remove the borders of a square matrix, I couldn't find a challenge to output the borders of a given matrix, so here it is.
Challenge:
Given a rectangular-shaped matrix containing integers as input, output its borders.
Challenge rules:
- The input is guaranteed to be a rectangular-shaped matrix (or empty)
- The input can contain negative and duplicated integers
- The output format and type is flexible and can be in any order (e.g.
[[1,2,3],[4,5,6],[7,8,9]]
may result in[1,2,3,4,6,7,8,9]
; or[[1,2,3],[6,9],[8,7],[4]]
; or9\n8\n7\n6\n4\n3\n2\n1\n
; etc.). As long as it's clear that just the border of the input-matrix is output, almost every output-format is allowed. (If you're unsure whether an output format is allowed, feel free to ask in the comments.)
General Rules:
- This is code-golf, so the shortest answer in bytes wins.
Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language. - Standard rules apply for your answer with default I/O rules, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.
- Default Loopholes are forbidden.
- If possible, please add a link with a test for your code (e.g. TIO).
- Also, adding an explanation for your answer is highly recommended.
2xN
orNx2
example to the test cases. \$\endgroup\$[1]
,[2]
, etc. for the images.. I changed the[8]
and[9]
image ids to80
and90
so I won't have to change the test case.. :/ \$\endgroup\$