Skip to content

Instantly share code, notes, and snippets.

@T-Dnzt
Last active November 3, 2018 03:00
Show Gist options
  • Save T-Dnzt/2b6e8d74167e07783a689ba4fe8ef9ff to your computer and use it in GitHub Desktop.
Save T-Dnzt/2b6e8d74167e07783a689ba4fe8ef9ff to your computer and use it in GitHub Desktop.

Write a program to transform the JSON provided in #Input into the one shown in #Output. Basically, each child should be placed in the children array of its parent (as defined by the parent_id).

Submit one solution using the language of your choice and one using Elixir. If you don't know Elixir yet, you will need to learn just enough for this challenge. Note that not being able to complete the Elixir implementation is okay, just send what you were able to write and explain what blocked you.

Keep track of the amount of hours you spend for each implementation and send them with your submission to thibault@omisego.co.

Input

{"0": 
  [{"id": 10,
    "title": "House",
    "level": 0,
    "children": [],
    "parent_id": null}],
 "1": 
  [{"id": 12,
    "title": "Red Roof",
    "level": 1,
    "children": [],
    "parent_id": 10},
   {"id": 18,
    "title": "Blue Roof",
    "level": 1,
    "children": [],
    "parent_id": 10},
   {"id": 13,
    "title": "Wall",
    "level": 1,
    "children": [],
    "parent_id": 10}],
 "2": 
  [{"id": 17,
    "title": "Blue Window",
    "level": 2,
    "children": [],
    "parent_id": 12},
   {"id": 16,
    "title": "Door",
    "level": 2,
    "children": [],
    "parent_id": 13},
   {"id": 15,
    "title": "Red Window",
    "level": 2,
    "children": [],
    "parent_id": 12}]}

Output

[{"id": 10,
  "title": "House",
  "level": 0,
  "children": 
   [{"id": 12,
     "title": "Red Roof",
     "level": 1,
     "children": 
      [{"id": 17,
        "title": "Blue Window",
        "level": 2,
        "children": [],
        "parent_id": 12},
       {"id": 15,
        "title": "Red Window",
        "level": 2,
        "children": [],
        "parent_id": 12}],
     "parent_id": 10},
    {"id": 18,
     "title": "Blue Roof",
     "level": 1,
     "children": [],
     "parent_id": 10},
    {"id": 13,
     "title": "Wall",
     "level": 1,
     "children": 
      [{"id": 16,
        "title": "Door",
        "level": 2,
        "children": [],
        "parent_id": 13}],
     "parent_id": 10}],
  "parent_id": null}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment