34
4
This challenge was inspired by a question on Mathematica.SE.
Say you've got a nested list/array of some arbitrary structure (the lists at each level don't necessarily have the same length). For simplicity, we'll assume that the nodes are non-negative integers or empty arrays. As an example
[[[1, 3], 2], [1, 4], 12, [[0, [], 0], [5, [7]]]]
Sometimes it's more convenient to flatten that list to perform some manipulation of the nodes, e.g.
--> [1, 3, 2, 1, 4, 12, 0, 0, 5, 7]
--> [1, 1, 0, 1, 0, 0, 0, 0, 1, 1]
But in the end you actually want to preserve the original structure, so you want to turn this back into
--> [[[1, 1], 0], [1, 0], 0, [[0, [], 0], [1, [1]]]
Your task is to perform that last step.
Given a nested list of arbitrary non-negative integers, which represents the desired structure of the result, and a flat list of non-negative integers, which represent the desired values, reshape the flat list into the form of the structured list. You may assume that both lists contain the same number of integers.
As usual you don't have to deal with invalid input (e.g. the second list not being flat, the input being syntactically malformed, not having integers as nodes, etc.). You may modify the input arrays in your code.
You may write a function or program, taking input via STDIN, command-line argument or function argument, and you may return the result or print it to STDOUT. You may use any convenient list or string format to represent input and output (as long as the format is unambiguous and the input is not preprocessed). Also, the format of both inputs needs to be consistent (so you can't take one input as a string and the other as a list, for instance). You may take the input lists in either order, but please specify the exact input method in your answer.
One more restriction: you must not use regular expressions. This is an array manipulation challenge, not a string manipulation challenge.
This is code golf, so the shortest answer (in bytes) wins.
Test Cases
Structure Values Result
[[[1,3],2],[1,4],12,[[0,0],[5,[7]]]] [1,1,0,1,0,0,0,0,1,1] [[[1,1],0],[1,0],0,[[0,0],[1,[1]]]]
[[[0,0],0],[0,0],0,[[0,0],[0,[0]]]] [1,1,0,1,0,0,0,0,1,1] [[[1,1],0],[1,0],0,[[0,0],[1,[1]]]]
[] [] []
[[]] [] [[]]
[0,1,2,3] [5,1,0,5] [5,1,0,5]
[[[[[0]]]]] [123] [[[[[123]]]]]
[0,[1,[]],[[]],[2,3],[]] [1,6,1,8] [1,[6,[]],[[]],[1,8],[]]
Is it allowed if the values in the Structure array get changed? – ProgramFOX – 2015-01-03T10:00:11.803
@ProgramFOX yes. "You may modify the input arrays in your code." – Martin Ender – 2015-01-03T11:05:45.077
Ironically, one of the submissions here is in Mathematica. – Isiah Meadows – 2015-01-05T19:50:13.723
1@impinball That is mine, which I posted along with the question, in order to prevent anyone else from stealing the answer from the linked question (and in fact, it's merely a golfed down version of that answer). – Martin Ender – 2015-01-05T20:31:57.733
@MartinBüttner Oh. Nice. It's actually one of the shorter answers, too. – Isiah Meadows – 2015-01-05T23:30:35.243