MetaMath
Mix.install([
{:jason, "~> 1.4"},
{:kino, "~> 0.8.0", override: true},
{:youtube, github: "brooklinjazz/youtube"},
{:hidden_cell, github: "brooklinjazz/hidden_cell"}
])
Navigation
MetaMath
You’re creating a math game. Players will be given math questions in the format "2 + 2".
The string can contain multiple operations in any order such as "2 + 10 * 23 - 10"
You need to determine the correct result to check with the player’s answer.
Math.correct?("2 + 2", 4) # true
Math.correct?("2 + 2", 5) # false
Example Solution
defmodule Math do
def correct?(equation, player_answer) do
{result, _bindings} = Code.eval_string(equation)
result == player_answer
end
end
Implement the Math module as documented.
defmodule Math do
@doc """
Determine if a player's answer matches the result of the math expression in the string.
iex> Math.correct?("1 + 1", 2)
true
iex> Math.correct?("1 + 1", 4)
false
iex> Math.correct?("1 - 1", 0)
true
iex> Math.correct?("10 + 14 * 4 - 2", 64)
true
"""
def correct?(equation, player_answer) do
end
end
Mark As Completed
file_name = Path.basename(Regex.replace(~r/#.+/, __ENV__.file, ""), ".livemd")
save_name =
case Path.basename(__DIR__) do
"reading" -> "meta_math_reading"
"exercises" -> "meta_math_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 meta-math-exercise
$ git add .
$ git commit -m "finish meta math exercise"
$ git push origin meta-math-exercise
Create a pull request from your meta-math-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 |
|---|---|
| Metaprogramming | Custom Assertions |