FizzBuzz
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
FizzBuzz
In the Elixir cell below create a Module FizzBuzz
with a function run/1
which takes in a range.
Enumerate through the integers and return a new list where
-
integers divisible by
3
are replaced withfizz
-
integers divisible by
5
are replaced withbuzz
-
integers divisible by
3
and5
are replaced withfizzbuzz
- integers not matching the above stay the same.
For example,
FizzBuzz.run(1..15)
[1, 2, "fizz", 4, "buzz", "fizz", 7, 8, "fizz", "buzz", 11, "fizz", 13, 14, "fizzbuzz"]
ExUnit.start(auto_run: false)
defmodule Assertion do
use ExUnit.Case
test "" do
defmodule FizzBuzz do
def run(range) do
end
end
defmodule Solutions.FizzBuzz do
def run(range) do
Enum.map(range, fn n ->
cond do
rem(n, 15) == 0 -> "fizzbuzz"
rem(n, 3) == 0 -> "fizz"
rem(n, 5) == 0 -> "buzz"
true -> n
end
end)
end
end
assert FizzBuzz.run(1..15) == [
1,
2,
"fizz",
4,
"buzz",
"fizz",
7,
8,
"fizz",
"buzz",
11,
"fizz",
13,
14,
"fizzbuzz"
]
assert FizzBuzz.run(1..100) == Solutions.FizzBuzz.run(1..100)
end
end
ExUnit.run()
# Make variables and modules defined in the test available.
# Also allows for exploration using the output of the cell.
defmodule FizzBuzz do
def run(range) do
end
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 fizzbuzz exercise"