Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Family Tree

exercises/family_tree.livemd

Family Tree

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

Navigation

Home Report An Issue Shopping ListMazes

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.

Commit Your Progress

DockYard Academy now recommends you use the latest Release rather than forking or cloning our repository.

Run git status to ensure there are no undesirable changes. Then run the following in your command line from the curriculum folder to commit your progress.

$ git add .
$ git commit -m "finish Family Tree exercise"
$ git push

We’re proud to offer our open-source curriculum free of charge for anyone to learn from at their own pace.

We also offer a paid course where you can learn from an instructor alongside a cohort of your peers. We will accept applications for the June-August 2023 cohort soon.

Navigation

Home Report An Issue Shopping ListMazes