Powered by AppSignal & Oban Pro

Timeline

exercises/timeline.livemd

Timeline

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

Timeline

You are building an application which creates a timeline of events. Users provide you with a list of Dates. You will provide the number of days between each date.

dates = [~D[2020-01-01], ~D[2020-01-02], ~D[2020-01-22]]

Timeline.from_dates(dates)
[1, 20]

BONUS: Timeline.from_strings/1.

As a bonus, implement the Timeline.from_strings/1 function which accepts a list of strings instead of a list of Dates.

date_strings = ["2020-01-01", "2020-01-02", "2020-01-22"]

Timeline.from_strings(date_strings)
[1, 20]

Example Solution

defmodule Timeline do
  
  def from_dates(dates) do
    Enum.chunk_every(dates, 2, 1, :discard)
    |> Enum.map(fn chunk ->
      [start, finish] = chunk
      Date.diff(finish, start)
    end)
  end

  def from_strings(date_strings) do
    date_strings
    |> Enum.map(&string_to_date/1)
    |> from_dates()
  end

  defp string_to_date(date_string) do
    [year, month, day] = String.split(date_string, "-")
    Date.new!(String.to_integer(year), String.to_integer(month), String.to_integer(day))
  end
end

Enter your solution below.

defmodule Timeline do
  @moduledoc """
  Documentation for `Timeline`
  """

  @doc """
  Given a list of [Date](https://hexdocs.pm/elixir/Date.html)s return a timeline of days between each date.

  ## Examples

    iex> Timeline.from_dates([~D[2020-01-01]])
    []

    iex> Timeline.from_dates([~D[2020-01-01], ~D[2020-01-02]])
    [1]

    iex> Timeline.from_dates([~D[2020-01-01], ~D[2020-01-02], ~D[2020-01-22]])
    [1, 20]
    
    iex> Timeline.from_dates([~D[2020-01-01], ~D[2020-01-10], ~D[2020-01-22], ~D[2020-01-24]])
    [9, 12, 2]
  """
  def from_dates(dates) do
  end

  @doc """
  Given a list of "YYYY-MM-DD" strings return a timeline of days between each date.
  Assume you will always be given two or more strings.

  ## Examples

    iex> Timeline.from_strings(["2020-01-01", "2020-01-02"])
    [1]

    iex> Timeline.from_strings(["2020-01-01", "2020-01-02", "2020-01-22"])
    [1, 20]
    
    iex> Timeline.from_strings(["2020-01-01", "2020-01-10", "2020-01-22", "2020-01-24"])
    [9, 12, 2]
  """
  def from_strings(date_strings) do
  end
end

Mark As Completed

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

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

Create a pull request from your timeline-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
Itinerary Strings and Binaries