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

Measurements

exercises/measurements.livemd

Measurements

Mix.install([
  {:jason, "~> 1.4"},
  {:kino, "~> 0.9", override: true},
  {:youtube, github: "brooklinjazz/youtube"},
  {:hidden_cell, github: "brooklinjazz/hidden_cell"}
])

Navigation

Home Report An Issue Named Number ListsBuilt-In Elixir Modules

Measurements

You are going to make a utility module for working with measurements. This exercise was inspired by Advent of Code 2021, which is a fantastic resource for learning about Elixir through practical problems.

Example Solution

defmodule Measurements do
  def increments(measurements) do
    measurements
    |> Enum.chunk_every(2, 1, :discard)
    |> Enum.map(fn [x, y] -> y - x end)
  end

  def increased(measurements) do
    measurements
    |> increments()
    |> Enum.count(&(&1 > 0))
  end

  def increased_by(measurements) do
    measurements
    |> increments()
    |> Enum.filter(&(&1 > 0))
    |> Enum.sum()
  end

  def average(measurements) do
    div(Enum.sum(measurements), Enum.count(measurements))
  end
end

Implement the Measurements module according to the documented function examples.

defmodule Measurements do
  @doc """
  Given a list of measurements as integers, determine the increment between each integer.

  ## Examples

    iex> Measurements.increments([100, 150, 120, 130])
    [50, -30, 10]

    iex> Measurements.increments([10, 20, 10, 40])
    [10, -10, 30]
  """
  def increments(measurements) do
    measurements
    |> Enum.chunk_every(2, 1, :discard)
    |> Enum.map(fn [x, y] -> y-x end)
  end

  @doc """
  Given a list of measurements as integers, determine how many have increased.
  Each positive change since the previous integer in the list should count as an increase.

  ## Examples

    iex> Measurements.increased([1, 1, 2, 3, 1])
    2

    iex> Measurements.increased([1, 2, 1])
    1

    iex> Measurements.increased([3, 1, 2])
    1
  """
  def increased(measurements) do
    measurements
    |> increments()
    |> Enum.count(&(&1 > 0))
  end

  @doc """
  Given a list of measurements as integers, sum the amount they have increased by.
  Only positive increments are taken into account.

  ## Examples

    iex> Measurements.increased_by([100, 150, 120, 130])
    60

    iex> Measurements.increased_by([10, 20, 10, 40])
    40
  """
  def increased_by(measurements) do
    measurements
    |> increments()
    |> Enum.filter(&(&1 > 0))
    |> Enum.sum()
  end

  @doc """
  Find the average of a list of numbers.
  You can calculate average as sum of integers / length of list.

  ## Examples

    iex> Measurements.average([4, 5, 6])
    5.0

    iex> Measurements.average([2, 10])
    6.0
  """
  def average(measurements) do
    Enum.sum(measurements) / Enum.count(measurements)
  end

end

Commit Your Progress

DockYard Academy now recommends you use the latest Release rather than forking or cloning our repository.

Run git status to ensure there are no undesirable changes. Then run the following in your command line from the curriculum folder to commit your progress.

$ git add .
$ git commit -m "finish Measurements exercise"
$ git push

We’re proud to offer our open-source curriculum free of charge for anyone to learn from at their own pace.

We also offer a paid course where you can learn from an instructor alongside a cohort of your peers. We will accept applications for the June-August 2023 cohort soon.

Navigation

Home Report An Issue Named Number ListsBuilt-In Elixir Modules