Skip to content

Commit e097304

Browse files
committed
PCC45 GeoffRiley
1 parent 9786701 commit e097304

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

45/GeoffRiley/fizzbuzz.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# -*- coding: utf-8 -*-
2+
from numbers import Number
3+
from typing import Union
4+
5+
6+
def fizzbuzz(val: Number) -> Union[str, Number]:
7+
if not isinstance(val, Number) or val % 1 != 0:
8+
raise ValueError('Only whole integer values accepted')
9+
if val % 15 == 0:
10+
return "FizzBuzz"
11+
if val % 5 == 0:
12+
return "Buzz"
13+
if val % 3 == 0:
14+
return "Fizz"
15+
return val
16+
17+
# Playing FizzBuzz
18+
# when passed the value 3
19+
# ✓ it says "Fizz"
20+
# when passed a multiple of 3
21+
# ✓ it says "Fizz"
22+
# when passed the value 5
23+
# ✓ it says "Buzz"
24+
# when passed a multiple of 5
25+
# ✓ it says "Buzz"
26+
# when passed a multiple of 3 and 5
27+
# ✓ it says "FizzBuzz"
28+
# when passed a value divisible by neither 3 nor 5
29+
# ✓ it says the value back unchanged
30+
# when passed a none whole integer value
31+
# ✓ it screams and raises a ValueError
32+
# when pass a none numeric value
33+
# ✓ it screams and raises a ValueError

45/GeoffRiley/fizzbuzz_spec.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from expects import expect, equal, raise_error
4+
from mamba import description, context, it
5+
6+
from fizzbuzz import fizzbuzz
7+
8+
with description('Playing FizzBuzz') as self:
9+
with context('when passed the value 3'):
10+
with it('says "Fizz"'):
11+
expect(fizzbuzz(3)).to(equal('Fizz'))
12+
13+
with context('when passed a multiple of 3'):
14+
with it('says "Fizz"'):
15+
expect(fizzbuzz(9)).to(equal('Fizz'))
16+
17+
with context('when passed the value 5'):
18+
with it('says "Buzz"'):
19+
expect(fizzbuzz(5)).to(equal('Buzz'))
20+
21+
with context('when passed a multiple of 5'):
22+
with it('says "Buzz"'):
23+
expect(fizzbuzz(20)).to(equal('Buzz'))
24+
25+
with context('when passed a multiple of 3 and 5'):
26+
with it('says "FizzBuzz"'):
27+
expect(fizzbuzz(15)).to(equal('FizzBuzz'))
28+
29+
with context('when passed a value divisible by neither 3 nor 5'):
30+
with it('says the value back unchanged'):
31+
expect(fizzbuzz(1)).to(equal(1))
32+
expect(fizzbuzz(2)).to(equal(2))
33+
34+
with context('when passed a none whole integer value'):
35+
with it('screams and raises a ValueError'):
36+
# Use lambda to overcome deficiency in expects library
37+
expect(lambda: fizzbuzz(3.14)).to(raise_error(ValueError))
38+
39+
with context('when pass a none numeric value'):
40+
with it('screams and raises a ValueError'):
41+
# Use lambda to overcome deficiency in expects library
42+
expect(lambda: fizzbuzz('fred')).to(raise_error(ValueError))

0 commit comments

Comments
 (0)