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

Voter Count

voter_count.livemd

Voter Count

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

Voter Count

You’re creating a system for counting votes.

Count Votes

In the Elixir cell below,

  • Create a module VoterCount with a count/2 function.
  • count/2 should accept a list of atoms that represent votes, and an atom.
  • count/2 should return an integer with the number of votes in the first parameter list that match the second parameter atom

For example:

VoterCount.count([:dogs, :dogs, :dogs, :cats], :dogs)
3

VoterCount.count([:dogs, :dogs, :dogs, :cats], :cats)
1

VoterCount.count([:apples, :oranges, :apples, :cats], :birds)
0
ExUnit.start(auto_run: false)

defmodule Assertion do
  use ExUnit.Case

  test "" do
    defmodule VoterCount do
    end

    assert VoterCount.count([], :test), "Implement the `count` function."

    assert VoterCount.count([:dogs, :dogs, :dogs, :cats], :dogs) == 3,
           "failed on ([:dogs, :dogs, :dogs, :cats], :dogs)"

    assert VoterCount.count([:dogs, :dogs, :dogs, :cats], :cats) == 1,
           "Failed on ([:dogs, :dogs, :dogs, :cats], :cats)"

    assert VoterCount.count([:apples, :oranges, :apples, :cats], :birds) == 0,
           "Failed on ([:apples, :oranges, :apples, :cats], :birds)"

    list = Enum.map(1..10, fn _ -> Enum.random([:cat, :dog, :bird, :apple, :orange]) end)
    choice = Enum.random([:cat, :dog, :bird, :apple, :orange])
    assert VoterCount.count(list, choice) == Enum.count(list, fn each -> each == choice end)
  end
end

ExUnit.run()

# Make variables and modules defined in the test available.
# Also allows for exploration using the output of the cell.
defmodule VoterCount do
end

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 voter count exercise"