Skip to content

Latest commit

 

History

History
237 lines (172 loc) · 7.63 KB

shopping_list.livemd

File metadata and controls

237 lines (172 loc) · 7.63 KB

Shopping List

Mix.install([
  {:youtube, github: "brooklinjazz/youtube"},
  {:hidden_cell, github: "brooklinjazz/hidden_cell"},
  {:tested_cell, github: "brooklinjazz/tested_cell"},
  {:utils, path: "#{__DIR__}/../utils"}
])

Navigation

Return Home Report An Issue

Setup

Ensure you type the ea keyboard shortcut to evaluate all Elixir cells before starting. Alternatively you can evaluate the Elixir cells as you read.

Text Shopping List

You are creating a shopping list app. Users have the ability to add items into their shopping_cart. Each item is represented as a string.

In the Elixir cells below, use ++ and -- to add the items shown.

  • Add "grapes", "walnuts", and "apples" to the shopping_cart
  • Add "blueberries", "chocolate", and "pizza" to the shopping_cart.
  • Remove "grapes" and "walnuts" from the shopping_cart
  • Add three "banana"s to the shopping_cart
Example solution
shopping_cart = []

shopping_cart = shopping_cart ++ ["grapes", "walnuts", "apples"]
shopping_cart = shopping_cart ++ ["blueberries", "chocolate", "pizza"]
shopping_cart = shopping_cart -- ["grapes", "walnuts"]
shopping_cart = shopping_cart ++ ["banana", "banana", "banana"]

Enter your solution below.

ExUnit.start(auto_run: false)

defmodule Assertion do
  use ExUnit.Case

  test "Shopping Cart" do
    try do
      Process.flag(:trap_exit, true)
      shopping_cart = [1]
      list = [] ++ ["grapes", "walnuts", "apples"]
      list = list ++ ["blueberries", "chocolate", "pizza"]
      list = list -- ["grapes", "walnuts"]
      list = list ++ ["banana", "banana", "banana"]

      assert is_list(shopping_cart), "Ensure shopping_list is still a list."

      assert shopping_cart == list
    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 received: #{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

            Also 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
<hr/>"))
    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.
shopping_cart = [1]

Text Shopping List With Quantities

Users of your shopping list app have asked that they be able to include the quantity of each item to make adding many items easier.

In the Elixir cell below, use a keyword list in the format [item: quantity] to add or remove items from the shopping_cart

  • Add 1 milk and 12 eggs
  • Add 2 bars_of_butter and 10 candies
  • Remove 2 bars_of_butter
  • Remove 5 candies (Notice 5 and not 10!).
Example solution
shopping_cart = []

shopping_cart = shopping_cart ++ [milk: 1, eggs: 12]
shopping_cart = shopping_cart ++ [bars_of_butter: 2, candies: 10]
shopping_cart = shopping_cart -- [bars_of_butter: 2]
shopping_cart = shopping_cart -- [candies: 10]
shopping_cart = shopping_cart ++ [candies: 5]
ExUnit.start(auto_run: false)

defmodule Assertion do
  use ExUnit.Case

  test "Shopping Cart With Quantities" do
    try do
      Process.flag(:trap_exit, true)
      shopping_cart = []
      list = [] ++ [milk: 1, eggs: 12]
      list = list ++ [bars_of_butter: 2, candies: 10]
      list = list -- [bars_of_butter: 2]
      list = list -- [candies: 10]
      list = list ++ [candies: 5]

      assert is_list(shopping_cart), "Ensure shopping_list is still a list."

      assert shopping_cart == list
    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 received: #{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

            Also 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
<hr/>"))
    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.
shopping_cart = []

Commit Your Progress

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

$ git add .
$ git commit -m "finish shopping list exercise"

Up Next

Previous Next
Maps Family Tree