12
Everybody loves nested lists! However, sometimes it's hard to make a nested list. You have to decide if you want to nest it deeper, or if you need to nest it shallower. So for your challenge, you must "Autonest" a list. To autonest a list, compare every pair of items in the list.
If the second item is smaller, separate the two elements by inserting closing and opening brackets between them, like this:
} { {2 , 1}For example,
{2, 1}becomes{2}, {1}, and{3, 2, 1}becomes{3}, {2}, {1}If the second item is the same, then change nothing. For example,
{1, 1, 1}stays the same, and{2, 1, 1, 1}would become{2}, {1, 1, 1}.If the second item is larger, then nest every following item one level deeper. For example,
{1, 2}would become{1, {2}}and{1, 2, 3}would become{1, {2, {3}}}
The Challenge
You must write a program or function that takes in a list of numbers, and returns the same list after being autonested. Take this input in your languages native list format (or the closest alternative) or as a string. You do not have to use curly braces like I did in my examples. You can use whichever type of brackets are most natural in your language, as long as this is consistent. You can safely assume the list will only contain integers. You can also assume the list will have at least 2 numbers in it. Here is some sample IO:
{1, 3, 2} --> {1, {3}, {2}}
{1, 2, 3, 4, 5, 6} --> {1, {2, {3, {4, {5, {6}}}}}}
{6, 5, 4, 3, 2, 1} --> {6}, {5}, {4}, {3}, {2}, {1}
{7, 3, 3, 2, 6, 4} --> {7}, {3, 3}, {2, {6}, {4}}
{7, 3, 1, -8, 4, 8, 2, -9, 2, 8} --> {7}, {3}, {1}, {-8, {4, {8}, {2}, {-9, {2, {8}}}}}
Standard loopholes apply, and the shortest answer in bytes wins!
2Can we take the input in our language's string format? – Downgoat – 2016-06-28T01:49:21.833
What is the max size of the integer? – thepiercingarrow – 2016-06-28T04:00:02.197
@thepiercingarrow I don't really care too much. It won't be anything ridiculous. You should be able to at least handle
[-100, 100]but I'm not planning on giving gigantic inputs. – James – 2016-06-28T04:59:45.547"If the second item is smaller, then nest all following elements one level higher, by inserting a closing bracket. Then, to make sure all the brackets stay matched, insert an opening bracket. For example,
{2, 1}becomes{2}, {1}" How is that one level higher? One level higher would be{2}, 1. What you have is the same level. – msh210 – 2016-06-28T07:30:06.377@msh210 Yeah, that was a poor explanation. Is the current phrasing better? – James – 2016-06-28T07:45:44.157
Yes, except that now it sounds like the result has to be a string, whereas previously I thought it could be a nested array of arrays. – msh210 – 2016-06-28T14:14:47.497
If I return a string, does it need to include spaces between the commas and braces? – Yytsi – 2016-06-28T14:34:32.760
@TuukkaX No, it just has to be a valid array representation. – James – 2016-06-28T15:05:04.257
@mbomb007 yes, you should handle negative numbers. – James – 2016-06-28T19:11:31.220
Can I use space separated list? – Akangka – 2016-06-29T11:20:57.140
In my answer: the output would not be interpreted as a nested list in MATL. It would in other languages, and it satisfies the output specification in the challenge. Is that accepted? That is, produce a string that can be interpreted as nested list/array (for example in other other languages), even if it doesn't have that meaning in the chosen language – Luis Mendo – 2016-06-29T23:25:09.600