Skip to content

Instantly share code, notes, and snippets.

@clamytoe
Last active December 9, 2017 01:15
Show Gist options
  • Save clamytoe/224ca962fc3c5297de986a41a226f221 to your computer and use it in GitHub Desktop.
Save clamytoe/224ca962fc3c5297de986a41a226f221 to your computer and use it in GitHub Desktop.
FizzBuzz challenge built with TDD...
def check_value(value):
"""
Validates the value that was passed into the program.
:param value: A positive integer is the only valid value
:return: int or an exception is raised
"""
if isinstance(value, bool):
raise ValueError
elif isinstance(value, str):
raise ValueError
elif isinstance(value, float):
raise ValueError
elif value <= 0:
raise ValueError
else:
try:
tested_value = int(value)
return tested_value
except ValueError:
raise ValueError
except TypeError:
raise TypeError
def fizz_check(num):
"""
Determines the correct FiiBuzz value to return for the int given.
If the number is divisible by 3 'Fizz' is returned.
If the number is divisible by 5 'Buzz' is returned.
If the number is divisible by both 3 and 5, 'FizzBuzz' is returned.
If none of those are true, then number is just returned.
:param num: A positive integer
:return: 'Fizz', 'Buzz', 'FizzBuzz', or the num given
"""
value = check_value(num)
return 'Fizz' * (value % 3 == 0) + 'Buzz' * (value % 5 == 0) or value
def fizz_gen(limit):
"""
FizzBuzz generator.
:param limit: An integer representing the last number to be processed
:return: Integer or String returned after going through fizz_check()
"""
max_num = check_value(limit)
for num in range(1, max_num + 1):
yield fizz_check(num)
def fizzbuzz(limit=100):
"""
Super overcomplicated implementation of the FizzBuzz challenge.
:param limit: Default value of numbers to process
:return: Standard Out response after being processed
"""
value = check_value(limit)
gen = fizz_gen(value)
for _ in range(value):
print(next(gen))
if __name__ == '__main__':
fizzbuzz()
from fizzbuzz import check_value, fizzbuzz, fizz_check, fizz_gen
from pytest import raises
def test_check_value():
""" Tests for invalid values """
with raises(ValueError):
check_value(True)
with raises(ValueError):
check_value(0)
with raises(ValueError):
check_value(-3)
with raises(ValueError):
fizz_check('15')
with raises(ValueError):
fizz_check(15.4563)
with raises(TypeError):
check_value(list())
with raises(ValueError):
fizz_check('Hello')
def test_fizz_check():
""" Tests for valid values """
assert fizz_check(1) == 1
assert fizz_check(3) == 'Fizz'
assert fizz_check(5) == 'Buzz'
assert fizz_check(15) == 'FizzBuzz'
assert fizz_check(1287341875) == 'Buzz'
def test_fizz_gen():
""" Tests for valid generator behavior """
numbers = fizz_gen(2)
assert next(numbers) == 1
assert next(numbers) == 2
with raises(StopIteration):
next(numbers)
word = fizz_gen('World!')
with raises(ValueError):
next(word)
def test_fizzbuzz(capsys):
""" Sample valid test run """
fizzbuzz(15)
out, err = capsys.readouterr()
assert out == ('1\n'
'2\n'
'Fizz\n'
'4\n'
'Buzz\n'
'Fizz\n'
'7\n'
'8\n'
'Fizz\n'
'Buzz\n'
'11\n'
'Fizz\n'
'13\n'
'14\n'
'FizzBuzz\n')
@clamytoe
Copy link
Author

clamytoe commented Dec 9, 2017

I know, got carried away...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment