21
Sometimes, I have lists of constant matrices in my code:
[ [[1, 0],
[0, 1]],
[[1, 0],
[0,-1]],
[[0, 1],
[1, 0]],
[[0,-1],
[1, 0]] ]
That's a terrible use of screen real estate. I'd much rather write them next to each other:
[ [[1, 0], [[1, 0], [[0, 1], [[0,-1],
[0, 1]], [0,-1]], [1, 0]], [1, 0]] ]
You'll find that this is still a syntactically valid nested list, it's just no longer rectangular and it has a very different structure (in particular, it gets deeper every time I add a matrix larger than 1x1). However, it's still possible to reconstruct the initial list of matrices from this new list.
So that I can use this syntax in the future, I need you to write some code which converts arrays that were written in a horizontal arrangement to the list of matrices they represent.
To make sure that answers don't perform 2D pattern matching on the layout in the source code, the input will be given either as just the array object, or if you take a string representation, then it will not contain any whitespace indicating how the literal was written in the code. So you'd get some input like this:
[[[1, 0], [[1, 0], [[0, 1], [[0,-1], [0, 1]], [0,-1]], [1, 0]], [1, 0]]]
And the output should be the following array or its string representation (again, no further layout is needed):
[[[1, 0], [0, 1]], [[1, 0], [0,-1]], [[0, 1], [1, 0]], [[0,-1], [1, 0]]]
This is the first and easier part of a two-part challenge. In this one, you may assume that all matrices are square and have the same dimensions and that they are properly aligned next to each other. In the second part we'll relax these assumptions.
Rules
The input will be a nested list or its canonical string representation (in your language of choice), and you should output the result in the same format. The result will always contain at least one matrix, and the matrices can be as small as 1x1. The matrices will only contain (signed) integers with absolute value less than 128.
You may write a program or a function and use any of the standard methods of receiving input and providing output.
You may use any programming language, but note that these loopholes are forbidden by default.
This is code-golf, so the shortest valid answer – measured in bytes – wins.
Test Cases
Each test case has a) the list matrices laid out nicely next to each other as they would be in the code (this is not your input), b) the unformatted list without the extraneous whitespace (this is your input), c) the expected output.
Pretty: [ [[0]] ]
Input: [[[0]]]
Output: [[[0]]]
Pretty: [ [[-1]], [[0]], [[1]] ]
Input: [[[-1]],[[0]],[[1]]]
Output: [[[-1]],[[0]],[[1]]]
Pretty: [ [[1, 0], [[1, 0], [[0, 1], [[0,-1],
[0, 1]], [0,-1]], [1, 0]], [1, 0]] ]
Input: [[[1,0],[[1,0],[[0,1],[[0,-1],[0,1]],[0,-1]],[1,0]],[1,0]]]
Output: [[[1,0],[0,1]],[[1,0],[0,-1]],[[0,1],[1,0]],[[0,-1],[1,0]]]
Pretty: [ [[1, 0, 0], [[ 127, 63, 31], [[1, 0, 0], [[0, 0, 0],
[0, 1, 0], [ 15, 0, -15], [0, 0, 1], [0, 0, 0],
[0, 0, 1]], [ -31, -63, -127]], [0, 1, 0]], [0, 0, 0]] ]
Input: [[[1,0,0],[[127,63,31],[[1,0,0],[[0,0,0],[0,1,0],[15,0,-15],[0,0,1],[0,0,0],[0,0,1]],[-31,-63,-127]],[0,1,0]],[0,0,0]]]
Output: [[[1,0,0],[0,1,0],[0,0,1]],[[127,63,31],[15,0,-15],[-31,-63,-127]],[[1,0,0],[0,0,1],[0,1,0]],[[0,0,0],[0,0,0],[0,0,0]]]
1I think I might know what inspired this challenge... – Neil – 2017-05-21T12:39:35.127
Is
[([1, 0], [0, 1]), ([1, 0], [0, -1]), ([0, 1], [1, 0]), ([0, -1], [1, 0])]
valid output for the third testcase? It is mixed list and tuple. – ovs – 2017-05-21T12:40:50.047@ovs No, sorry. Since input and output format should match, the corresponding input would be
[([1,0], ([1, 0}, ...
and that would give you additional information. – Martin Ender – 2017-05-21T12:51:28.760@Neil what inspired this challenge? – caird coinheringaahing – 2017-05-21T15:22:12.697
@RandomUser The desire to golf all the things. Least amount of screen real estate wins! – Dennis – 2017-05-21T15:23:39.173
@RandomUser this did.
– Martin Ender – 2017-05-21T15:32:51.433