Created
March 22, 2023 18:05
-
-
Save bennett39/4579b2e0b7da6ab51cf1fe57d0f289d4 to your computer and use it in GitHub Desktop.
An implementation of fizzbuzz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fizzbuzz(stop: int): | |
"""On multiples of 3, print 'fizz' | |
On multiples of 5, print 'buzz' | |
On multiples of both, print 'fizzbuzz' | |
For all other numbers, print the number | |
""" | |
result = [] | |
for x in range(1, stop+1): | |
if x % 15 == 0: | |
result.append('fizzbuzz') | |
elif x % 3 == 0: | |
result.append('fizz') | |
elif x % 5 == 0: | |
result.append('buzz') | |
else: | |
result.append(x) | |
return result | |
test_cases = ( | |
(-1, []), | |
(0, []), | |
(1, [1]), | |
(3, [1, 2, 'fizz']), | |
(15, [1, 2, 'fizz', 4, 'buzz', 'fizz', 7, 8, 'fizz', 'buzz', 11, 'fizz', 13, 14, 'fizzbuzz']), | |
) | |
for stop, expected in test_cases: | |
actual = fizzbuzz(stop) | |
try: | |
assert actual == expected | |
print('PASS!') | |
except: | |
print(actual, expected) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment