repl.it
@shahinism/

FizzBuzz Python

Python

No description

fork
loading
Files
  • main.py
  • Packager files
  • poetry.lock
  • pyproject.toml
  • requirements.txt
main.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from nose.tools import assert_equal, assert_raises


def is_divisible(number, by):
    return number % by == 0


class Solution(object):
    def fizz_buzz(self, number):
        if not isinstance(number, int):
            raise TypeError
        elif number <= 0:
            raise ValueError

        return [self.process_number(n) for n in range(1, number + 1)]

    @staticmethod
    def process_number(number):
        if is_divisible(number, 3) and is_divisible(number, 5):
            return "FizzBuzz"
        elif is_divisible(number, 3):
            return "Fizz"
        elif is_divisible(number, 5):
            return "Buzz"

        return str(number)


class TestFizzBuzz(object):

    def test_fizz_buzz(self):
        solution = Solution()
        assert_raises(TypeError, solution.fizz_buzz, None)
        assert_raises(ValueError, solution.fizz_buzz, 0)
        expected = [
            '1',
            '2',
            'Fizz',
            '4',
            'Buzz',
            'Fizz',
            '7',
            '8',
            'Fizz',
            'Buzz',
            '11',
            'Fizz',
            '13',
            '14',
            'FizzBuzz'
        ]
        assert_equal(solution.fizz_buzz(15), expected)
        print('Success: test_fizz_buzz')


def main():
    test = TestFizzBuzz()
    test.test_fizz_buzz()


main()
Fetching token
?