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.
Given a rectangular-shaped matrix containing integers as input, output its borders.
- 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
9\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.)
- 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.