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

Metric Conversion

exercises/metric_conversion.livemd

Metric Conversion

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 Replacing NilsGuards

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 value meters
millimeter 1.0 0.001
centimeter 1.0 0.01
meter 1.0 1.0
kilometer 1.0 1000.0

Users provide values using {from_unit, value} tuples, and the desired unit to convert to.

Metric.convert({:centimeter, 100.0}, :meter)
1.0

Units can be :millimeter, :centimeter, :meter, or :kilometer. Values are always floats.

Example Solution

defmodule Metric do
  def convert(from, to) do
    meters = to_meter(from)

    case to do
      :millimeter -> meters / 0.001
      :centimeter -> meters / 0.01
      :meter -> meters / 1.0
      :kilometer -> meters / 1000.0
    end
  end

  defp to_meter({unit, value}) do
    case unit do
      :millimeter -> value * 0.001
      :centimeter -> value * 0.01
      :meter -> value * 1.0
      :kilometer -> value * 1000.0
    end
  end
end
defmodule Metric do
  def convert(from, to) do
  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 Metric Conversion 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 Replacing NilsGuards