Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions 45/GeoffRiley/fizzbuzz.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# -*- coding: utf-8 -*-
from numbers import Number
from typing import Union


def fizzbuzz(val: Number) -> Union[str, Number]:
if not isinstance(val, Number) or val % 1 != 0:
raise ValueError('Only whole integer values accepted')
if val % 15 == 0:
return "FizzBuzz"
if val % 5 == 0:
return "Buzz"
if val % 3 == 0:
return "Fizz"
return val

# Playing FizzBuzz
# when passed the value 3
# ✓ it says "Fizz"
# when passed a multiple of 3
# ✓ it says "Fizz"
# when passed the value 5
# ✓ it says "Buzz"
# when passed a multiple of 5
# ✓ it says "Buzz"
# when passed a multiple of 3 and 5
# ✓ it says "FizzBuzz"
# when passed a value divisible by neither 3 nor 5
# ✓ it says the value back unchanged
# when passed a none whole integer value
# ✓ it screams and raises a ValueError
# when pass a none numeric value
# ✓ it screams and raises a ValueError
42 changes: 42 additions & 0 deletions 45/GeoffRiley/fizzbuzz_spec.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# -*- coding: utf-8 -*-

from expects import expect, equal, raise_error
from mamba import description, context, it

from fizzbuzz import fizzbuzz

with description('Playing FizzBuzz') as self:
with context('when passed the value 3'):
with it('says "Fizz"'):
expect(fizzbuzz(3)).to(equal('Fizz'))

with context('when passed a multiple of 3'):
with it('says "Fizz"'):
expect(fizzbuzz(9)).to(equal('Fizz'))

with context('when passed the value 5'):
with it('says "Buzz"'):
expect(fizzbuzz(5)).to(equal('Buzz'))

with context('when passed a multiple of 5'):
with it('says "Buzz"'):
expect(fizzbuzz(20)).to(equal('Buzz'))

with context('when passed a multiple of 3 and 5'):
with it('says "FizzBuzz"'):
expect(fizzbuzz(15)).to(equal('FizzBuzz'))

with context('when passed a value divisible by neither 3 nor 5'):
with it('says the value back unchanged'):
expect(fizzbuzz(1)).to(equal(1))
expect(fizzbuzz(2)).to(equal(2))

with context('when passed a none whole integer value'):
with it('screams and raises a ValueError'):
# Use lambda to overcome deficiency in expects library
expect(lambda: fizzbuzz(3.14)).to(raise_error(ValueError))

with context('when pass a none numeric value'):
with it('screams and raises a ValueError'):
# Use lambda to overcome deficiency in expects library
expect(lambda: fizzbuzz('fred')).to(raise_error(ValueError))