Friday 26 August 2016

Friday code puzzle

G'day:

I'm waiting for a blood test and I'm "customer number 649", and they are currently processing "customer number 541". I have dubbed this place Franz Kafka Memorial Hospital.

Anyway, I have an hour or two to kill, so writing this quicky on my phone.

Last week on the CFML Slack Channel we piloted the notion of a Friday Puzzle. It went quite well, so we're continuing it. Don't be put off by the fact it's the CFML Slack Channel: we actively encourage our community to use any language they like, or different languages from usual, as it's a good way to improve one's skills as a programmer. I'll be doing this week's one in PHP. And maybe diversifying from there into something else.

This week's puzzle is as follows (in summary):

Challenge:

Convert a flat representation of a hierarchical data into a nested tree structure.

[...]

dataset:

idparentIdnodeText
1nullFile
2 1New
32File
42Folder
5 nullEdit
65Copy
75Cut
85Paste
9nullHelp
109About


Expected result:


[ 
    { 
        nodeText : "File",
        children : [ 
            { 
                nodeText : "New",
                children : [ 
                    {nodeText: "File"},
                    {nodeText: "Folder"}
                ] 
            }
        ] 
    },{ 
        nodeText : "Edit",
            children : [ 
                {nodeText: "Copy"},
                {nodeText: "Cut"},
                {nodeText: "Paste"}
            ]
    },{ 
        nodeText: "Help",
            children : [
                { nodeText: "About"}
            ]
    } 
]


I've omitted the rules and expectations and stuff. Full details are on the Slack Channel. You can sign-up at: http://cfml-slack.herokuapp.com/;

Give it a go!

Oh... And they're now processing customer 585.

Righto.

--
Adam