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

Mad Libs

mad_libs.livemd

Mad Libs

Mix.install([
  {:kino, github: "livebook-dev/kino", override: true},
  {:kino_lab, "~> 0.1.0-dev", github: "jonatanklosko/kino_lab"},
  {:vega_lite, "~> 0.1.4"},
  {:kino_vega_lite, "~> 0.1.1"},
  {:benchee, "~> 0.1"},
  {:ecto, "~> 3.7"},
  {:math, "~> 0.7.0"},
  {:faker, "~> 0.17.0"},
  {:utils, path: "#{__DIR__}/../utils"},
  {:tested_cell, git: "https://github.com/BrooklinJazz/tested_cell"}
])

Navigation

Return Home Report An Issue

String Concatenation Madlib

In this exercise, you’re going to recreate the following madlib game. A madlib is a game where the player can enter in a word or sentence to fill in the blanks.

In the Elixir cell below bind name_of_company, a_defined_offering, a_defined_audience, solve_a_problem, and secret_sauce to a string.

Then use string concatenation to concentenate the variables to create the madlib above.

ExUnit.start(auto_run: false)

defmodule Assertion do
  use ExUnit.Case

  test "Exercise" do
    try do
      Process.flag(:trap_exit, true)
      name_of_company = "DockYard"
      a_defined_offering = "DockYard Academy"
      a_defined_audience = "students"
      solve_a_problem = "learn Elixir"
      secret_sauce = "Livebook"

      madlib =
        "My company, " <>
          name_of_company <>
          ", is developing " <>
          a_defined_offering <>
          " to help " <>
          a_defined_audience <> " " <> solve_a_problem <> " with " <> secret_sauce <> "."

      assert is_binary(name_of_company) and name_of_company != "",
             "name_of_company should be a non-empty string"

      assert is_binary(a_defined_offering) and a_defined_offering != "",
             "a_defined_offering should be a non-empty string"

      assert is_binary(a_defined_audience) and a_defined_audience != "",
             "a_defined_audience should be a non-empty string"

      assert is_binary(solve_a_problem) and solve_a_problem != "",
             "solve_a_problem should be a non-empty string"

      assert is_binary(secret_sauce) and secret_sauce != "",
             "secret_sauce should be a non-empty string"

      assert String.contains?(madlib, name_of_company),
             "name_of_company should be used in the madlib"

      assert String.contains?(madlib, a_defined_offering),
             "a_defined_offering should be used in the madlib"

      assert String.contains?(madlib, a_defined_audience),
             "a_defined_audience should be used in the madlib"

      assert String.contains?(madlib, solve_a_problem),
             "solve_a_problem should be used in the madlib"

      assert String.contains?(madlib, secret_sauce), "secret_sauce should be used in the madlib"

      madlib =
        "My company, " <>
          name_of_company <>
          ", is developing " <>
          a_defined_offering <>
          " to help " <>
          a_defined_audience <> " " <> solve_a_problem <> " with " <> secret_sauce <> "."
    catch
      error ->
        flunk("""
          Your solution threw the following error:

          #{inspect(error)}
        """)

      :exit, {error, {GenServer, message_type, [_pid, message, _timeout]}} ->
        flunk("""
            GenServer crashed with the following error:

            #{inspect(error)}

            When it recieved: #{inspect(message)} #{message_type}

            Likely you need to define the corresponding handler for #{inspect(message)}.

            Ensure you defined a `handle_call/3`, `handle_info/2`, or `handle_cast/2` or appropriate handler function.

              def handle_call(:message, _from, state) do
                ...
              end

            Then ensure you call `GenServer.call/2`, `GenServer.cast/2`, or otherwise send the message correctly.

              GenServer.call(pid, :message)
        """)

      :exit, error ->
        flunk("""
          Unhandled exit with the following error:

          #{inspect(error)}
        """)
    after
      # all warnings and errors are printed to the previous Kino Frame
      # to avoid cluttering the test results display.
      Process.sleep(10)
      Kino.render(Kino.Markdown.new("### Test Results 

"
)) end end end ExUnit.run() # Make variables and modules defined in the test available. # Also allows for exploration using the output of the cell. # Unfortunately, this results in duplication of warnings. name_of_company = "DockYard" a_defined_offering = "DockYard Academy" a_defined_audience = "students" solve_a_problem = "learn Elixir" secret_sauce = "Livebook" madlib = "My company, " <> name_of_company <> ", is developing " <> a_defined_offering <> " to help " <> a_defined_audience <> " " <> solve_a_problem <> " with " <> secret_sauce <> "."
name_of_company = "DockYard"
a_defined_offering = "DockYard Academy"
a_defined_audience = "students"
solve_a_problem = "learn Elixir"
secret_sauce = "Livebook"

madlib =
  "My company, " <>
    name_of_company <>
    ", is developing " <>
    a_defined_offering <>
    " to help " <> a_defined_audience <> " " <> solve_a_problem <> " with " <> secret_sauce <> "."

String Interpolation Madlib

In the Elixir cell below, fill in blank1, blank2, and blank3 with a word to replace the blanks in the sentence.

> A programmer is a that turns into __.

Then use string interpololation to interpolate these values into a single string bound to the madlib variable.

ExUnit.start(auto_run: false)

defmodule Assertion do
  use ExUnit.Case

  test "Exercise" do
    try do
      Process.flag(:trap_exit, true)
      blank1 = "person"
      blank2 = "code"
      blank3 = "apps"

      madlib =
        "A programmer is a " <> blank1 <> " that turns " <> blank2 <> " into " <> blank3 <> "."

      assert is_binary(blank1) and blank1 != "", "blank1 should be a non-empty string"
      assert is_binary(blank2) and blank2 != "", "blank2 should be a non-empty string"
      assert is_binary(blank3) and blank3 != "", "blank3 should be a non-empty string"
      assert madlib == "A programmer is a #{blank1} that turns #{blank2} into #{blank3}."
    catch
      error ->
        flunk("""
          Your solution threw the following error:

          #{inspect(error)}
        """)

      :exit, {error, {GenServer, message_type, [_pid, message, _timeout]}} ->
        flunk("""
            GenServer crashed with the following error:

            #{inspect(error)}

            When it recieved: #{inspect(message)} #{message_type}

            Likely you need to define the corresponding handler for #{inspect(message)}.

            Ensure you defined a `handle_call/3`, `handle_info/2`, or `handle_cast/2` or appropriate handler function.

              def handle_call(:message, _from, state) do
                ...
              end

            Then ensure you call `GenServer.call/2`, `GenServer.cast/2`, or otherwise send the message correctly.

              GenServer.call(pid, :message)
        """)

      :exit, error ->
        flunk("""
          Unhandled exit with the following error:

          #{inspect(error)}
        """)
    after
      # all warnings and errors are printed to the previous Kino Frame
      # to avoid cluttering the test results display.
      Process.sleep(10)
      Kino.render(Kino.Markdown.new("### Test Results 

"
)) end end end ExUnit.run() # Make variables and modules defined in the test available. # Also allows for exploration using the output of the cell. # Unfortunately, this results in duplication of warnings. blank1 = "person" blank2 = "code" blank3 = "apps" madlib = "A programmer is a " <> blank1 <> " that turns " <> blank2 <> " into " <> blank3 <> "."

Commit Your Progress

Run the following in your command line from the project folder to track and save your progress in a Git commit.

$ git add .
$ git commit -m "finish mad libs exercise"