Skip to content

Instantly share code, notes, and snippets.

@tedmiston
Last active November 10, 2015 03:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tedmiston/6f076273beb6f6be133d to your computer and use it in GitHub Desktop.
Save tedmiston/6f076273beb6f6be133d to your computer and use it in GitHub Desktop.
Fizz buzz one-liner
"""
Fizz buzz "one liner".
Disclaimer: I don't write code like this for real. Python 2/3.
"""
from __future__ import print_function
def fizz_buzz(start=1, end=100, word1='Fizz', word2='Buzz'): [print(' '.join([word1, word2]) if i % 3 == 0 and i % 5 == 0 else (word1 if i % 3 == 0 else (word2 if i % 5 == 0 else i))) for i in range(start, end + 1)]
if __name__ == '__main__':
fizz_buzz()
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
Fizz Buzz
16
17
Fizz
19
Buzz
Fizz
22
23
Fizz
Buzz
26
Fizz
28
29
Fizz Buzz
31
32
Fizz
34
Buzz
Fizz
37
38
Fizz
Buzz
41
Fizz
43
44
Fizz Buzz
46
47
Fizz
49
Buzz
Fizz
52
53
Fizz
Buzz
56
Fizz
58
59
Fizz Buzz
61
62
Fizz
64
Buzz
Fizz
67
68
Fizz
Buzz
71
Fizz
73
74
Fizz Buzz
76
77
Fizz
79
Buzz
Fizz
82
83
Fizz
Buzz
86
Fizz
88
89
Fizz Buzz
91
92
Fizz
94
Buzz
Fizz
97
98
Fizz
Buzz
"""
Basic test suite for fizz buzz.
"""
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
import sys
import unittest
from fizz_buzz import fizz_buzz
class TestFizzBuzzBase(unittest.TestCase):
"""The base class makes working with a fizz buzz convenient."""
def setUp(self):
"""
Hijacks print function to capture stdout into a variable instead.
Needed because fizz buzz requirements include *printing* the values.
In a normal library, we'd implement fizz_buzz(...) to return instead.
"""
self.real_stdout = sys.stdout
self.result = StringIO()
sys.stdout = self.result
def _restore_stdout(self):
"""Let us print again."""
sys.stdout = self.real_stdout
def _parse_output(self):
"""Return the list printed to stdout as an string array."""
return self.result.getvalue().strip().split('\n')
def fizz_buzz_plus(self, *args, **kwargs):
"""
Run fizz buzz itself, parse output, and restore stdout.
Gathered in one method because every test needs to run each step.
"""
fizz_buzz(*args, **kwargs)
results = self._parse_output()
self._restore_stdout()
return results
class TestFizzBuzz(TestFizzBuzzBase):
"""Ensure the fizzes fizz and the buzzes buzz in the standard case."""
def test_fizz(self):
"""Test a number that should fizz."""
results = self.fizz_buzz_plus()
first_fizz = 3
first_fizz_idx = first_fizz - 1
self.assertEqual(results[first_fizz_idx], 'Fizz')
def test_buzz(self):
"""Test a number that should buzz."""
results = self.fizz_buzz_plus()
first_buzz = 5
first_buzz_idx = first_buzz - 1
self.assertEqual(results[first_buzz_idx], 'Buzz')
def test_fizz_buzz(self):
"""Test a number that should fizz buzz."""
results = self.fizz_buzz_plus()
first_fizz_buzz = 15
first_fizz_buzz_idx = first_fizz_buzz - 1
self.assertEqual(results[first_fizz_buzz_idx], 'Fizz Buzz')
def test_number(self):
"""Test a number that should not fizz and/or buzz."""
results = self.fizz_buzz_plus()
first_number = 1
first_number_idx = first_number - 1
self.assertEqual(results[first_number_idx], '1')
def test_one_to_a_hundred(self):
"""Test the standard 1 to 100 sequence."""
results = self.fizz_buzz_plus()
with open('results_expected.txt') as f:
expected = f.read().strip().split('\n')
self.assertEqual(results, expected)
class TestFizzBuzzCustom(TestFizzBuzzBase):
"""Ensure the fizzes fizz and the buzzes buzz with custom params."""
def test_custom_word1(self):
"""Test changing the first word."""
w1 = 'taco'
results = self.fizz_buzz_plus(word1=w1)
first_fizz = 3
first_fizz_idx = first_fizz - 1
self.assertEqual(results[first_fizz_idx], w1)
def test_custom_word2(self):
"""Test changing the second word."""
w2 = 'burrito'
results = self.fizz_buzz_plus(word2=w2)
first_buzz = 5
first_buzz_idx = first_buzz - 1
self.assertEqual(results[first_buzz_idx], w2)
def test_custom_start(self):
"""Test with a reasonable custom start index."""
start = 10
results = self.fizz_buzz_plus(start=start)
with open('results_expected.txt') as f:
expected = f.read().strip().split('\n')
start_idx = start - 1
expected = expected[start_idx:] # clip results before n=10
self.assertEqual(results, expected)
def test_custom_end(self):
"""Test with a reasonable custom end index."""
end = 30
results = self.fizz_buzz_plus(end=end)
with open('results_expected.txt') as f:
expected = f.read().strip().split('\n')
expected = expected[:end] # clip results after n=30
self.assertEqual(results, expected)
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment