Metric Conversion
Mix.install([
{:jason, "~> 1.4"},
{:kino, "~> 0.8.0", override: true},
{:youtube, github: "brooklinjazz/youtube"},
{:hidden_cell, github: "brooklinjazz/hidden_cell"}
])
Navigation
Metric Conversion
You’re going to build an app to convert metric measurements such as millimeters, centimeters, meters, and kilometers according to the following table.
[
[unit: :millimeter, value: 1, meter: 0.001],
[unit: :centimeter, value: 1, meter: 0.01],
[unit: :meter, value: 1, meter: 1],
[unit: :kilometer, value: 1, meter: 1000]
]
|> Kino.DataTable.new()
Users provide values using {from_unit, value} tuples, and the desired unit to convert to.
Metric.convert({:centimeter, 100}, :meter)
1.0
The unit to convert from, and the unit to convert to can be either :millimeter, :centimeter, :meter, or :kilometer. The output should always be a float.
defmodule Metric do
@moduledoc """
Documentation for `Metric`
"""
@doc """
Convert one measurement to another.
We've used math values in the result
## Examples
Convert unit to meters.
iex> Metric.convert({:millimeter, 1}, :meter)
0.001
iex> Metric.convert({:centimeter, 1}, :meter)
0.01
iex> Metric.convert({:meter, 1}, :meter)
1.0
iex> Metric.convert({:kilometer, 1}, :meter)
1000
Convert meters to unit.
iex> Metric.convert({:meter, 1}, :millimeter)
1000.0
iex> Metric.convert({:meter, 1}, :centimeter)
100.0
iex> Metric.convert({:meter, 1}, :meter)
1.0
iex> Metric.convert({:meter, 1}, :kilometer)
0.001
Convert unit to unit.
iex> Metric.convert({:centimeter, 1000}, :kilometer)
0.01
iex> Metric.convert({:millimeter, 10}, :centimeter)
1.0
iex> Metric.convert({:kilometer, 10}, :centimeter)
1000000
"""
def convert(from, to) do
end
end
Mark As Completed
file_name = Path.basename(Regex.replace(~r/#.+/, __ENV__.file, ""), ".livemd")
save_name =
case Path.basename(__DIR__) do
"reading" -> "metric_conversion_reading"
"exercises" -> "metric_conversion_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 metric-conversion-exercise
$ git add .
$ git commit -m "finish metric conversion exercise"
$ git push origin metric-conversion-exercise
Create a pull request from your metric-conversion-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 |
|---|---|
| Rock Paper Scissors Pattern Matching | Guards |