Pascal’s Triangle
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
Setup
Ensure you type the ea
keyboard shortcut to evaluate all Elixir cells before starting. Alternatively you can evaluate the Elixir cells as you read.
Pascal’s Triangle
In this exercise, you’re going to generate Pascal’s Triangle for a certain number of rows.
In Pascal’s Triangle, each number is the sum of the two integers above it.
flowchart
a[1]
b1[1]
b2[1]
c1[1]
c2[2]
c3[1]
d1[1]
d2[3]
d3[3]
d4[1]
e1[1]
e2[4]
e3[6]
e4[4]
e5[1]
a --> b1
a --> b2
b1 --> c1
b1 --> c2
b2 --> c2
b2 --> c3
c1 --> d1
c1 --> d2
c2 --> d2
c2 --> d3
c3 --> d3
c3 --> d4
d1 --> e1
d1 --> e2
d2 --> e2
d2 --> e3
d3 --> e3
d3 --> e4
d4 --> e4
d4 --> e5
We can also represent Pascal’s triangle as a list of lists.
[[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 3, 6, 3, 1]]
In the Elixir cell below, finish the of/1
function in the Pascal
module which will
return Pascal’s triangle in a list for n
number of rows.
Pascal.of(1)
[
[1]
]
Pascal.of(2)
[
[1],
[1, 1],
]
Pascal.of(5)
[
[1],
[1, 1],
[1, 2, 1],
[1, 3, 3, 1],
[1, 4, 6, 4, 1]
]
Your Solution
defmodule Pascal do
def of(n) do
end
end
Utils.feedback(:pascal, Pascal)
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 pascals triangle exercise"