Skip to content

Instantly share code, notes, and snippets.

@lucasrcezimbra
Created September 22, 2016 23:56
Show Gist options
  • Save lucasrcezimbra/29f715d017231b6d19662d9d48453f3c to your computer and use it in GitHub Desktop.
Save lucasrcezimbra/29f715d017231b6d19662d9d48453f3c to your computer and use it in GitHub Desktop.
FizzBuzz in Python
"""
Regras do FizzBuzz
1. Se a posição for multipla de 3: fizz
2. Se a posição for multipla de 5: buzz
3. Se a posição for multipla de 3 e 5: fizzbuzz
4. Para qualquer outra posição fale o próprio nº.
"""
from functools import partial
multiple_of = lambda base, num: num % base == 0
multiple_of_3 = partial(multiple_of, 3)
multiple_of_5 = partial(multiple_of, 5)
def robot(pos):
out = str(pos)
if multiple_of_3(pos) and multiple_of_5(pos):
out = 'fizzbuzz'
elif multiple_of_5(pos):
out = 'buzz'
elif multiple_of_3(pos):
out = 'fizz'
return out
import unittest
from fizzbuzz import robot
class FizzBuzzTest(unittest.TestCase):
def test_say_1_when_1(self):
self.assertEqual(robot(1), '1')
def test_say_2_when_2(self):
self.assertEqual(robot(2), '2')
def test_say_fizz_when_3(self):
self.assertEqual(robot(3), 'fizz')
def test_say_buzz_when_5(self):
self.assertEqual(robot(5), 'buzz')
def test_say_fizz_when_6(self):
self.assertEqual(robot(6), 'fizz')
def test_say_buzz_when_10(self):
self.assertEqual(robot(10), 'buzz')
def test_say_fizzbuzz_when_15(self):
self.assertEqual(robot(15), 'fizzbuzz')
def test_say_buzz_when_30(self):
self.assertEqual(robot(30), 'fizzbuzz')
"""
def assert_equal(result, expected):
from sys import _getframe
if not result == expected:
msg = 'Fail: Line {} got {} expecting {}'
line_no = _getframe().f_back.f_lineno
print(msg.format(line_no, result, expected))
if __name__ == '__main__':
assert_equal(robot(1), '1')
assert_equal(robot(2), '2')
assert_equal(robot(3), 'fizz')
assert_equal(robot(5), 'buzz')
assert_equal(robot(6), 'fizz')
assert_equal(robot(10), 'buzz')
assert_equal(robot(15), 'fizzbuzz')
assert_equal(robot(30), 'fizzbuzz')
"""
"""
def assert_equal(result, expected):
from sys import _getframe
if not result == expected:
msg = 'Fail: Line {} got {} expecting {}'
line_no = _getframe().f_back.f_lineno
print(msg.format(line_no, result, expected))
if __name__ == '__main__':
assert_equal(robot(1), '1')
assert_equal(robot(2), '2')
assert_equal(robot(3), 'fizz')
assert_equal(robot(5), 'buzz')
assert_equal(robot(6), 'fizz')
assert_equal(robot(10), 'buzz')
assert_equal(robot(15), 'fizzbuzz')
assert_equal(robot(30), 'fizzbuzz')
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment