Powered by AppSignal & Oban Pro

Pascal's Triangle

exercises/pascals_triangle.livemd

Pascal’s Triangle

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

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, 4, 6, 4, 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]
]
defmodule Pascal do
  @doc ~S"""
  Generates a Pascal's Triangle of `n` rows.

  ## Examples

      iex> Pascal.of(1)
      [[1]]

      iex> Pascal.of(5)
      [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1]]

  """
  def of(n) do
  end
end

Mark As Completed

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

save_name =
  case Path.basename(__DIR__) do
    "reading" -> "pascals_triangle_reading"
    "exercises" -> "pascals_triangle_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 pascals-triangle-exercise
$ git add .
$ git commit -m "finish pascals triangle exercise"
$ git push origin pascals-triangle-exercise

Create a pull request from your pascals-triangle-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
Factorial Computer Hardware