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

Naming Numbers

naming_numbers.livemd

Naming Numbers

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

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.

Naming Numbers

In the Elixir cell below, create a function naming_numbers which accepts a single digit integer and returns its string representation. So 1 would become "one".

flowchart
   0 --> zero
   1 --> one
   2 --> two
   3 --> three
   4 --> four
   5 --> five
   6 --> six
   7 --> seven
   8 --> eight
   9 --> nine
naming_numbers.(1)
"one"

Replace nil with your anonymous function.

ExUnit.start(auto_run: false)

defmodule Assertion do
  use ExUnit.Case

  test "" do
    naming_numbers = nil

    assert is_function(naming_numbers),
           "Ensure you bind `naming_numbers` to an anonymous function."

    list = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]

    Enum.each(1..9, fn integer ->
      assert naming_numbers.(integer) == Enum.at(list, integer)
    end)
  end
end

ExUnit.run()

# Make variables and modules defined in the test available.
# Also allows for exploration using the output of the cell.
naming_numbers = nil

Numbering Names

In the Elixir cell below, create a function numbering_names which accepts a number’s name and returns its string representation. So "one" would become 1.

In addition to accepting lowercase name, also accept uppercase names so "One" would become 1.

flowchart
  zero --> 0
  one --> 1
  two --> 2
  three --> 3
  four --> 4
  five --> 5
  six --> 6
  seven --> 7
  eight --> 8
  nine --> 9
flowchart
  Zero --> 0
  One --> 1
  Two --> 2
  Three --> 3
  Four --> 4
  Five --> 5
  Six --> 6
  Seven --> 7
  Eight --> 8
  Nine --> 9

Replace nil with your anonymous function.

ExUnit.start(auto_run: false)

defmodule Assertion do
  use ExUnit.Case

  test "" do
    numbering_names = nil
    list = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
    capital_list = Enum.map(list, &String.capitalize/1)

    assert is_function(numbering_names),
           "Ensure you bind `naming_numbers` to an anonymous function."

    Enum.each(list, fn name ->
      assert numbering_names.(name) ==
               Enum.find_index(list, fn each -> each == String.downcase(name) end)
    end)

    Enum.each(capital_list, fn name ->
      assert numbering_names.(name) ==
               Enum.find_index(list, fn each -> each == String.downcase(name) end)
    end)
  end
end

ExUnit.run()

# Make variables and modules defined in the test available.
# Also allows for exploration using the output of the cell.
numbering_names = nil

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 naming numbers exercise"