Powered by AppSignal & Oban Pro

Family Tree

exercises/family_tree.livemd

Family Tree

Mix.install([
  {:jason, "~> 1.4"},
  {:kino, "~> 0.8.0", override: true},
  {:youtube, github: "brooklinjazz/youtube"},
  {:hidden_cell, github: "brooklinjazz/hidden_cell"}
])

Navigation

Return Home Report An Issue

Family Tree

Maps allow you to create tree like structures using keys and values. Thus it’s possible to make a family tree. In the Elixir cell below, create a family tree that is a map.

flowchart BT
p1g1[Grandparent]
p1g2[Grandparent]
p2g1[Grandparent]
p2g2[Grandparent]
p1[Parent]
p2[Parent]
c1[Child]

c1 --> p1
c1 --> p2
p1 --> p1g1
p1 --> p1g2
p2 --> p2g1
p2 --> p2g2

The map will start as a person with a :name, :age, :status, and :parents keys.

  • name is string.
  • age is an integer.
  • status will be an atom of :child, :parent, or :grandparent.
  • parents should be a list of maps with their own :name, :age, :status, and :parents keys.

In the Elixir cell below, create a map that represents the following family tree diagram.

classDiagram
    direction BT
    class Arthur {
        name: "Arthur"
        status: :child
        age: 22
    }
    class Uther {
        name: "Uther"
        status: :parent
        age: 56
    }
    class Ygraine {
        name: "Ygraine"
        status: :parent
        age: 45
    }
    class Han {
        name: "Han"
        status: :grand_parent
        age: 81
    }
    class Leia {
        name: "Leia"
        status: :grand_parent
        age: 82
    }
    class Bob {
        name: "Bob"
        status: :grand_parent
        age: 68
    }
    class Bridget {
        name: "Bridget"
        status: :grand_parent
        age: 70
    }

    Arthur --> Uther
    Arthur --> Ygraine
    Ygraine --> Bob
    Ygraine --> Bridget
    Uther --> Han
    Uther --> Leia

Example solution

This is an example of nested data.

%{
  name: "Arthur",
  status: :child,
  age: 22,
  parents: [
    %{
      name: "Uther",
      status: :parent,
      age: 56,
      parents: [
        %{name: "Han", status: :grand_parent, age: 81},
        %{name: "Leia", status: :grand_parent, age: 82}
      ]
    },
    %{
      name: "Ygraine",
      status: :parent,
      age: 68,
      parents: [
        %{name: "Bob", status: :grand_parent, age: 68},
        %{name: "Bridget", status: :grand_parent, age: 70}
      ]
    }
  ]
}

You might also consider extracting each family member as its own variable.

han = %{name: "Han", status: :grand_parent, age: 81}
leia = %{name: "Leia", status: :grand_parent, age: 82}
bob = %{name: "Bob", status: :grand_parent, age: 68}
bridget = %{name: "Bridget", status: :grand_parent, age: 70}

uther = %{name: "Uther", status: :parent, age: 56, parents: [han, leia]}
ygraine = %{name: "Ygraine", status: :parent, age: 68, parents: [bob, bridget]}

arthur = %{name: "Arthur", status: :child, age: 22, parents: [uther, ygraine]}

family_tree = arthur

Enter your solution below.

Mark As Completed

file_name = Path.basename(Regex.replace(~r/#.+/, __ENV__.file, ""), ".livemd")

save_name =
  case Path.basename(__DIR__) do
    "reading" -> "family_tree_reading"
    "exercises" -> "family_tree_exercise"
  end

progress_path = __DIR__ <> "/../progress.json"
existing_progress = File.read!(progress_path) |> Jason.decode!()

default = Map.get(existing_progress, save_name, false)

form =
  Kino.Control.form(
    [
      completed: input = Kino.Input.checkbox("Mark As Completed", default: default)
    ],
    report_changes: true
  )

Task.async(fn ->
  for %{data: %{completed: completed}} <- Kino.Control.stream(form) do
    File.write!(
      progress_path,
      Jason.encode!(Map.put(existing_progress, save_name, completed), pretty: true)
    )
  end
end)

form

Commit Your Progress

Run the following in your command line from the curriculum folder to track and save your progress in a Git commit. Ensure that you do not already have undesired or unrelated changes by running git status or by checking the source control tab in Visual Studio Code.

$ git checkout -b family-tree-exercise
$ git add .
$ git commit -m "finish family tree exercise"
$ git push origin family-tree-exercise

Create a pull request from your family-tree-exercise branch to your solutions branch. Please do not create a pull request to the DockYard Academy repository as this will spam our PR tracker.

DockYard Academy Students Only:

Notify your teacher by including @BrooklinJazz in your PR description to get feedback. You (or your teacher) may merge your PR into your solutions branch after review.

If you are interested in joining the next academy cohort, sign up here to receive more news when it is available.

Up Next

Previous Next
Shopping List Mazes