-
Notifications
You must be signed in to change notification settings - Fork 0
/
fizz_buzz.ex
30 lines (27 loc) · 947 Bytes
/
fizz_buzz.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
defmodule FizzBuzz do
@moduledoc """
Intuition/Approach
Started out knowing I wanted to enumerate over the
list witn an Enum.reduce() and the na simple case statment to generate
the list. After submission I realize its about 100ms slower than the
fastest one posted but in a real-world scenario I think this is readable and clean.
Visible here: https://leetcode.com/problems/fizz-buzz/solutions/3694632/elixir-enum-reduce-solution/
"""
@doc """
Function that iternates through `n` numbers and generates a list
that dictates which numbers are divisble by 5 and 3 with the words "Fizz" and "Buzz"
"""
@spec fizz_buzz(n :: integer) :: [String.t]
def fizz_buzz(n) do
Enum.reduce(1..n, [], fn num, acc ->
acc ++ [
case {rem(num, 3), rem(num, 5)} do
{0, 0} -> "FizzBuzz"
{0, _} -> "Fizz"
{_, 0} -> "Buzz"
_ -> to_string(num)
end
]
end)
end
end