Mix.install([
{:jason, "~> 1.4"},
{:kino, "~> 0.9", override: true},
{:youtube, github: "brooklinjazz/youtube"},
{:hidden_cell, github: "brooklinjazz/hidden_cell"}
])
In the previous Counting Votes exercise, we created a module to count a list of votes. For example, users would vote for their favorite animal to decide if :birds
, :dogs
, or :cats
were more popular.
Now, you're going to create a WeightedVoting.count/2
that can count the number of votes for a single value such as :dogs
or :cats
. provided in a keyword list.
WeightedVoting.count([cats: 10, dogs: 20, dogs: 20], :dogs)
40
You're also going to create a WeightedVoting.tally/1
function that will count the votes provided in a keyword list, and return the results as a keyword list like so.
WeightedVoting.tally([cats: 10, dogs: 20, dogs: 20])
[cats: 10, dogs: 40]
Example Solution
defmodule WeightedVoting do
def count(votes, vote_to_count) do
Enum.reduce(votes, 0, fn {key, value}, acc ->
if key == vote_to_count do
acc + value
else
acc
end
end)
end
def tally(votes) do
Enum.reduce(votes, [], fn {key, vote_count}, acc ->
Keyword.update(acc, key, vote_count, fn existing_value ->
existing_value + vote_count
end)
end)
end
def tally_map(votes) do
Enum.reduce(votes, %{}, fn {key, vote_count}, acc ->
Map.update(acc, key, vote_count, fn existing_value ->
existing_value + vote_count
end)
end)
end
end
Implement the WeightedVoting
module as documented below.
defmodule WeightedVoting do
@doc """
Count the total number of votes for some key in a keyword list.
## Examples
iex> WeightedVoting.count([dogs: 20, dogs: 10], :dogs)
30
iex> WeightedVoting.count([cats: 10, dogs: 20, dogs: 30], :dogs)
50
iex> WeightedVoting.count([cats: 10, dogs: 20, dogs: 10, cats: 30], :cats)
40
"""
def count(votes, vote_to_count) do
end
@doc """
Tally the total number of votes in a keyword list, and return
the results as a keyword list.
The keyword list should be sorted in alphabetical order.
## Examples
iex> WeightedVoting.tally([dogs: 20, dogs: 10])
[dogs: 30]
iex> WeightedVoting.tally([cats: 10, dogs: 20, dogs: 10])
[cats: 10, dogs: 30]
iex> WeightedVoting.tally([cats: 10, dogs: 20, cats: 20, dogs: 10, birds: 20])
[birds: 20, cats: 30, dogs: 30]
"""
def tally(votes) do
end
end
Create a WeightedVoting.tally_map/1
function which returns a map instead of a keyword list.
You may copy the following code into the WeightedVoting
module above to get started.
@doc """
Tally the total number of votes in a keyword list, and return
the results as a map.
## Examples
iex> WeightedVoting.tally_map([dogs: 20, dogs: 10])
%{dogs: 30}
iex> WeightedVoting.tally_map([cats: 10, dogs: 20, dogs: 10])
%{cats: 10, dogs: 30}
iex> WeightedVoting.tally_map([cats: 10, dogs: 20, cats: 20, dogs: 10, birds: 20])
%{birds: 20, cats: 30, dogs: 30}
"""
def tally_map(votes) do
end
DockYard Academy now recommends you use the latest Release rather than forking or cloning our repository.
Run git status
to ensure there are no undesirable changes.
Then run the following in your command line from the curriculum
folder to commit your progress.
$ git add .
$ git commit -m "finish Weighted Voting exercise"
$ git push
We're proud to offer our open-source curriculum free of charge for anyone to learn from at their own pace.
We also offer a paid course where you can learn from an instructor alongside a cohort of your peers. We will accept applications for the June-August 2023 cohort soon.