Mix.install([
{:youtube, github: "brooklinjazz/youtube"},
{:hidden_cell, github: "brooklinjazz/hidden_cell"},
{:tested_cell, github: "brooklinjazz/tested_cell"},
{:utils, path: "#{__DIR__}/../utils"}
])
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.
Run the following in your command line from the beta_curriculum folder to track and save your progress in a Git commit.
$ git add .
$ git commit -m "finish family tree exercise"
Previous | Next |
---|---|
Shopping List | Mazes |