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

Family Tree

family_tree.livemd

Family Tree

Mix.install([
  {:kino, github: "livebook-dev/kino", override: true},
  {:kino_lab, "~> 0.1.0-dev", github: "jonatanklosko/kino_lab"},
  {:vega_lite, "~> 0.1.4"},
  {:kino_vega_lite, "~> 0.1.1"},
  {:benchee, "~> 0.1"},
  {:ecto, "~> 3.7"},
  {:math, "~> 0.7.0"},
  {:faker, "~> 0.17.0"},
  {:utils, path: "#{__DIR__}/../utils"},
  {:tested_cell, git: "https://github.com/BrooklinJazz/tested_cell"}
])

Navigation

Return Home Report An Issue

Lesson

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
ExUnit.start(auto_run: false)

defmodule Assertion do
  use ExUnit.Case

  test "" do
    family_tree = %{}
    assert is_map(family_tree), "Ensure `family_tree` is a map."
    assert %{name: "Arthur"} = family_tree, "Ensure `family_tree` starts with Arthur."

    assert %{name: "Arthur", parents: parents} = family_tree,
           "Ensure Arthur in `family_tree` has a list of parents."

    assert length(parents) == 2, "Ensure both parents are included."
  end
end

ExUnit.run()

# Make variables and modules defined in the test available.
# Also allows for exploration using the output of the cell.
family_tree = %{}

Commit Your Progress

Run the following in your command line from the project folder to track and save your progress in a Git commit.

$ git add .
$ git commit -m "finish family tree exercise"