Measurements
Mix.install([
{:jason, "~> 1.4"},
{:kino, "~> 0.8.0", override: true},
{:youtube, github: "brooklinjazz/youtube"},
{:hidden_cell, github: "brooklinjazz/hidden_cell"}
])
Navigation
Return Home<span style=”padding: 0 30px”></span> Report An Issue
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 ```elixir 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 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 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
end
@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
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
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
iex> Measurements.average([2, 10])
6
"""
def average(measurements) do
end
end
Mark As Completed
file_name = Path.basename(Regex.replace(~r/#.+/, __ENV__.file, ""), ".livemd")
save_name =
case Path.basename(__DIR__) do
"reading" -> "measurements_reading"
"exercises" -> "measurements_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 measurements-exercise
$ git add .
$ git commit -m "finish measurements exercise"
$ git push origin measurements-exercise
Create a pull request from your measurements-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 |
|---|---|
| Counting Votes | A Safe Range |