Last week I had talked about a layout dilemma that my buddy Justin was having. This reminded me of an issue that programming challenge he hit me with a few months prior. The challenge was to take an object tree and generate the proper outline numbering for it. The object tree was hierarchical with an indefinite number of children and nesting levels. The object tree looks something like this:
The challenge didn’t get into indenting based on nesting, but that’s definitely something that could improved. The code itself relies on on recursion since the object can have any number of children which could have any number of children,
The gist is that I start with an array with a single element of zero. For each pass through the function, the last number in the array is incremented. For each increase in nesting, another number is added to the end. Then the array of numbers is combined together (with periods in between) to create the outline number. There’s also some magic to toss in “.0” to the top level numbers.
Got a better way to attack this one? Comment below!