Skip to content

Instantly share code, notes, and snippets.

@MartinHarding
Created October 31, 2021 17:55
Show Gist options
  • Save MartinHarding/abbe861c93e23609f5998b3a3f9fc5ee to your computer and use it in GitHub Desktop.
Save MartinHarding/abbe861c93e23609f5998b3a3f9fc5ee to your computer and use it in GitHub Desktop.
CracklePop
#!/usr/bin/env python
"""Save as: cracklepop.py
I also looked into doing it entirely with regex and ran across this very
interesting post (I confess I only partially understand it, but it's an
interesting discussion): https://stackoverflow.com/a/13223927
"""
# The normal one:
for num in range(1, 101):
if not num % 15:
print('CracklePop')
elif not num % 3:
print('Crackle')
elif not num % 5:
print('Pop')
else:
print(num)
# The cheeky (bash-curl-sed) one:
# curl -s 'https://raw.githubusercontent.com/Keith-S-Thompson/fizzbuzz-polyglot/master/expected-output.txt' | sed 's/Fizz/Crackle/g;s/Buzz/Pop/g'
# Testing the normal one with a version of the cheeky-one (assuming you have
# pytest installed, run the test with `pytest cracklepop.py`):
import re
import requests
def cracklepops(start=1, end=100):
result = []
for num in range(1, end + 1):
if not num % 15:
result.append('CracklePop')
elif not num % 3:
result.append('Crackle')
elif not num % 5:
result.append('Pop')
else:
result.append(str(num))
return result
def test_cracklepops():
url = 'https://raw.githubusercontent.com/Keith-S-Thompson/fizzbuzz-polyglot/master/expected-output.txt'
response = requests.get(url)
lines = [line.decode("utf-8") for line in response.iter_lines()]
expected_cracklepops = [
line.replace('Fizz', 'Crackle').replace('Buzz', 'Pop')
for line in lines
]
actual_cracklepops = cracklepops()
assert actual_cracklepops == expected_cracklepops
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment