Home Articles Categories Series
Pythonise Just now

Python FizzBuzz functions

Two function variations to solve the popular FizzBuzz interview question


Article Posted on by in Python
Julian Nash · 7 months ago in Python

Solving the "FizzBuzz" interview question in one of 2 ways.

Code

fizz_buzz.py

def fizz_buzz_v1(start, end):

    """
    Typical test interview question using simple if, elif else control flow. 
    Returns a string based on the "start" and "end" values provided,
    replacing specific values with the following:

    Multiples of 5 and 3 are replaced with "FizzBuzz",
    Multiples of 5 are replaced with "Buzz",
    Multiples of 3 are replaced with "Fizz",
    Any numbers not divisible by 3, 5 or both are replaced with the string version of themselves

    Note - +1 is added to the "end" parameter to include it in the return string

    Params:
        start (int): The start of the range
        end (int): The end of the range

    Returns:
        str: A comma-separated string, for example "1,2,Fizz,4,Buzz,Fizz"
    """

    result = []

    for i in range(start, end + 1):
        if i % 5 == 0 and i % 3 == 0:
            result.append("FizzBuzz")
        elif i % 5 == 0:
            result.append("Buzz")
        elif i % 3 == 0:
            result.append("Fizz")
        else:
            result.append(str(i))

    return ",".join(result)


def fizz_buzz_v2(start, end):

    """
    Typical test interview question using a (rather silly) list comprehension. 
    Returns a string based on the "start" and "end" values provided,
    replacing specific values with the following:

    Multiples of 5 and 3 are replaced with "FizzBuzz",
    Multiples of 5 are replaced with "Buzz",
    Multiples of 3 are replaced with "Fizz",
    Any numbers not divisible by 3, 5 or both are replaced with the string version of themselves

    Note - +1 is added to the "end" parameter to include it in the return string

    Params:
        start (int): The start of the range
        end (int): The end of the range

    Returns:
        str: A comma-separated string, for example "1,2,Fizz,4,Buzz,Fizz"
    """

    return ",".join(
        [
            "FizzBuzz" * (i % 5 == 0 and i % 3 == 0)
            or "Buzz" * (i % 5 == 0)
            or "Fizz" * (i % 3 == 0)
            or str(i)
            for i in range(start, end + 1)
        ]
    )

Tests

tests.py

import unittest
from fizz_buzz import fizz_buzz_v1, fizz_buzz_v2


class TestFizzBuzz(unittest.TestCase):

    # Testing fizz_buzz_v1 (if, elif, else)
    def test_fizz_buzz_v1(self):
        start = 1
        end = 5
        expected_result = "1,2,Fizz,4,Buzz"
        self.assertEqual(fizz_buzz_v1(start=start, end=end), expected_result)

    def test_fizz_buzz_v1_2(self):
        start = 1
        end = 15
        expected_result = "1,2,Fizz,4,Buzz,Fizz,7,8,Fizz,Buzz,11,Fizz,13,14,FizzBuzz"
        self.assertEqual(fizz_buzz_v1(start=start, end=end), expected_result)

    def test_fizz_buzz_v1_3(self):
        start = 1
        end = 999
        with open("fizz_buzz.txt", "r") as results:
            expected_result = results.read()
        self.assertEqual(fizz_buzz_v1(start=start, end=end), expected_result)

    # Testing fizz_buzz_v2 (List comprehension)
    def test_fizz_buzz_v2(self):
        start = 1
        end = 5
        expected_result = "1,2,Fizz,4,Buzz"
        self.assertEqual(fizz_buzz_v2(start=start, end=end), expected_result)

    def test_fizz_buzz_v2_2(self):
        start = 1
        end = 15
        expected_result = "1,2,Fizz,4,Buzz,Fizz,7,8,Fizz,Buzz,11,Fizz,13,14,FizzBuzz"
        self.assertEqual(fizz_buzz_v2(start=start, end=end), expected_result)

    def test_fizz_buzz_v2_3(self):
        start = 1
        end = 999
        with open("fizz_buzz.txt", "r") as results:
            expected_result = results.read()
        self.assertEqual(fizz_buzz_v2(start=start, end=end), expected_result)


if __name__ == "__main__":
    unittest.main()

Example

example.py

from fizz_buzz import fizz_buzz_v1, fizz_buzz_v2

start = 1
end = 20

print(fizz_buzz_v1(start, end))
print(fizz_buzz_v2(start, end))

Usage

Clone this repo & run:

python example.py

Output:

1,2,Fizz,4,Buzz,Fizz,7,8,Fizz,Buzz,11,Fizz,13,14,FizzBuzz,16,17,Fizz,19,Buzz
1,2,Fizz,4,Buzz,Fizz,7,8,Fizz,Buzz,11,Fizz,13,14,FizzBuzz,16,17,Fizz,19,Buzz

Testing

python tests.py
Last modified · 02 Apr 2019
Did you find this article useful?
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License
Contents
Loading...