Skip to content

Commit

Permalink
Add ShittyFizzBuzz statement generator
Browse files Browse the repository at this point in the history
  • Loading branch information
sambostock committed Apr 10, 2017
1 parent 9d691f9 commit 9dc6617
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
78 changes: 78 additions & 0 deletions lib/fizz_badd/shitty_fizz_buzz.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
defmodule FizzBadd.ShittyFizzBuzz do
@moduledoc """
Module providing functionality for generating a shitty Fizz Buzz implementation.
"""

alias FizzBadd.FizzBuzz, as: FizzBuzz

import FizzBuzz, only: [fizz_buzz: 1]

@doc """
Generate a number of if statements able used as part of a Fizz Buzz implementation.
The implementation consists of manually iterating over every number in a
range, with each if statement controlling next output.
Obviously this leads to an implementation with a maximum input size. This is
intended, as it contributes to the _shittiness_.
## Examples
iex> FizzBadd.ShittyFizzBuzz.statements(1)
["if n == 1, do: IO.puts \\"1\\""]
iex> FizzBadd.ShittyFizzBuzz.statements(3)
[
"if n >= 1, do: IO.write \\"1\\"",
"if n > 1, do: IO.write \\", \\"",
"if n >= 2, do: IO.write \\"2\\"",
"if n > 2, do: IO.write \\", \\"",
"if n == 3, do: IO.puts \\"Fizz\\""
]
"""
def statements(n) do
1..n
|> Enum.flat_map(&(statements_at_index(&1, n)))
end

@doc """
Produce the statements generating the Fizz Buzz at `index` in the Fizz Buzz
subsequence ending at `limit`.
## Examples
iex> FizzBadd.ShittyFizzBuzz.statements_at_index(1, 1)
["if n == 1, do: IO.puts \\"1\\""]
iex> FizzBadd.ShittyFizzBuzz.statements_at_index(1, 2)
[
"if n >= 1, do: IO.write \\"1\\"",
"if n > 1, do: IO.write \\", \\""
]
"""
def statements_at_index(index, limit) when index > limit do
raise ArgumentError, "index (#{index}) must be less than limit #{limit}"
end

def statements_at_index(index, limit) when index == limit do
[print_statement(index, final: true)]
end

def statements_at_index(index, limit) when index < limit do
[
print_statement(index),
comma_statement(index)
]
end

defp print_statement(n, final: true) do
"if n == #{n}, do: IO.puts \"#{fizz_buzz(n)}\""
end

defp print_statement(n) do
"if n >= #{n}, do: IO.write \"#{fizz_buzz(n)}\""
end

defp comma_statement(n) do
"if n > #{n}, do: IO.write \", \""
end
end
7 changes: 7 additions & 0 deletions test/fizz_badd/shitty_fizz_buzz_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
defmodule FizzBadd.ShittyFizzBuzzTest do
use ExUnit.Case, async: true

alias FizzBadd.ShittyFizzBuzz, as: ShittyFizzBuzz

doctest ShittyFizzBuzz
end

0 comments on commit 9dc6617

Please sign in to comment.