Skip to content

Instantly share code, notes, and snippets.

@robert-king
Created September 13, 2013 23:45
Show Gist options
  • Save robert-king/6557424 to your computer and use it in GitHub Desktop.
Save robert-king/6557424 to your computer and use it in GitHub Desktop.
from bisect import bisect_left
__author__ = 'Robert'
"""
Fizz Buzz and Zing on multiples of two, three and five.
Find the 1000th number that doesn't have a Fizz, Buzz or Zing.
"""
def two():
while True:
yield ""
yield "Fizz"
def three():
while True:
yield ""
yield ""
yield "Buzz"
def five():
exclamation_mark = False
while True:
yield ""
yield ""
yield ""
yield ""
yield "Zing!" if exclamation_mark else "Zing"
class BonusPoints(list):
def __getitem__(self, x):
return x - (x/2 + x/3 + x/5 - x/6 - x/10 - x/15 + x/30) #find min x s.t. f(x) = number
def find(self, number=1000):
#by default it finds the 1000th number that does not have any "fizz", "buzz" or "zing"
return bisect_left(self, number, hi=30*number)
def get_lines():
n = input()
assert(isinstance(n, int))
for values in zip(range(1, n + 1), two(), three(), five()):
line = "%s: %s%s%s" % values
yield line.strip()
yield "Bonus - 1000th = %s" % BonusPoints().find()
with open('output.txt', 'w') as out_file:
out_file.writelines("\n".join(get_lines()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment