Mix.install([
{:youtube, github: "brooklinjazz/youtube"},
{:hidden_cell, github: "brooklinjazz/hidden_cell"},
{:tested_cell, github: "brooklinjazz/tested_cell"},
{:utils, path: "#{__DIR__}/../utils"}
])
Ensure you type the ea
keyboard shortcut to evaluate all Elixir cells before starting. Alternatively you can evaluate the Elixir cells as you read.
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 theshopping_cart
- Add
"blueberries"
,"chocolate"
, and"pizza"
to theshopping_cart
. - Remove
"grapes"
and"walnuts"
from theshopping_cart
- Add three
"banana"
s to theshopping_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]
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
and12
eggs
- Add
2
bars_of_butter
and10
candies
- Remove
2
bars_of_butter
- Remove
5
candies
(Notice5
and not10
!).
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 = []
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"
Previous | Next |
---|---|
Maps | Family Tree |