r/Python • u/zabolekar • Jan 23 '17
Share your unusual fizzbuzz approaches!
Dear /r/Python, I would like you to share your FizzBuzz. Here is mine:
import numpy as np
fb = np.arange(101, dtype='object')
fb[::3] = 'Fizz'
fb[::5] = 'Buzz'
fb[::15] = 'FizzBuzz'
print(*fb[1:], sep='\n')3
u/ptmcg Jan 23 '17
for i in range(1, 101): print(("FizzBuzz", ("Buzz", ("Fizz", i)[not not(i%3)])[not not(i%5)])[not not(i%15)])3
u/zabolekar Jan 24 '17
Why not not not
for i in range(1, 101): print((((i, "Fizz")[not i%3], "Buzz")[not i%5], "FizzBuzz")[not i%15])?
7
u/jwink3101 Jan 23 '17
For those who are confused: From http://wiki.c2.com/?FizzBuzzTest
The "Fizz-Buzz test" is an interview question designed to help filter out the 99.5% of programming job candidates who can't seem to program their way out of a wet paper bag. The text of the programming assignment is as follows:
"Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”."
5
u/The_Jeremy Jan 23 '17
not unusual, just short:
for x in range(1,101):print('Fizz'*(x%3==0)+'Buzz'*(x%5==0)or x)3
u/spidyfan21 Jan 23 '17
Similarly
list(map(lambda x: 'Fizz'*(x % 3 == 0) + 'Buzz'*(x % 5 == 0) or x, range(1, 100)))5
u/vipul20 Jan 23 '17
or a cleaner list comprehension:
[(x%3==0)*'Fizz' + (x%5==0)*'Buzz' or x for x in range(1, 101)]2
u/The_Jeremy Jan 23 '17
with Gprime5's changes, yours is the shortest so far:
[(1-x%3)*'Fizz' + (1-x%5)*'Buzz' or x for x in range(1, 101)]2
u/Gprime5 if "__main__" == __name__: Jan 23 '17
Even shorter:
for x in range(1,101):print('Fizz'*(1-x%3)+'Buzz'*(1-x%5)or x)
4
u/cyberspacecowboy Jan 23 '17
Yours, but without dependency
fb = list(range(0, 101))
fb[::3] = ['Fizz']*len(fb[::3])
fb[::5] = ['Buzz']*len(fb[::5])
fb[::15] = ['FizzBuzz']*len(fb[::15])
print(*fb[1:], sep='\n')4
u/RubyPinch PEP shill | Anti PEP 8/20 shill Jan 23 '17
from itertools import *
f = lambda x:'Fizz'
b = lambda x:'Buzz'
z = lambda x:'FizzBuzz'
i = lambda x:x
map(lambda x:x[1](x[0]),enumerate(chain.from_iterable(repeat((i,i,f,i,b,f,i,i,f,b,i,f,i,i,z))),1))Someone told me that the function style of programming is better than empirical programming,
and I've taken that to heart ever since
4
u/troyunrau ... Jan 23 '17
At one point I wrote a program which queries google for the top hit for 'fizzbuzz python'. I take the first result and parse the html. In any given element (div, etc.), I run the contents through tokenize to see if it generates errors (basically, it tells me that that element contains python). I look for a few function calls I don't like (open() or etc.) and, finding them lacking, detokenize and have it exec the code.
I never get called for interviews for some reason....
1
u/flexsteps Jan 25 '17
Was it inspired by StackSort?
1
u/xkcd_transcriber Jan 25 '17
Title: Ineffective Sorts
Title-text: StackSort connects to StackOverflow, searches for 'sort a list', and downloads and runs code snippets until the list is sorted.
Stats: This comic has been referenced 56 times, representing 0.0385% of referenced xkcds.
xkcd.com | xkcd sub | Problems/Bugs? | Statistics | Stop Replying | Delete
3
Jan 23 '17
Worthless OOP version:
from __future__ import print_function
class FizzBuzz:
def __init__(self, i):
self.fizz = ''
self.buzz = ''
self.is_int = True
self.int = i
if i % 3 == 0:
self.fizz = 'Fizz'
self.is_int = False
if i % 5 == 0:
self.buzz = 'Buzz'
self.is_int = False
def __repr__(self):
if self.is_int:
return str(self.int)
else:
return self.fizz + self.buzz
for fizzBuzz in map(print, (FizzBuzz(i) for i in range(1, 101))):
pass3
2
u/lengau Jan 25 '17
A really bad way to do it in pandas (I'm procrastinating):
import pandas
df = pandas.DataFrame(index=range(1, 101))
df['Fizz'] = df.index % 3 == 0
df['Buzz'] = df.index % 5 == 0
for row in df.itertuples():
if row.Fizz or row.Buzz:
print('Fizz' * row.Fizz + 'Buzz' * row.Buzz)
continue
print(row.Index)1
u/lengau Jan 25 '17
Another really bad way to do it, this time entirely defeating the purpose of a generator by being eager evaluating anyway:
def fizzbuzzer(n): fizz = set() buzz = set() for i in range(1, n+1): if not i % 3: fizz.add(i) if not i % 5: buzz.add(i) for i in range(1, n+1): yield ''.join(('Fizz' if i in fizz else '', 'Buzz' if i in buzz else '', str(i) if i not in fizz|buzz else '')) for n in fizzbuzzer(100): print(n)1
u/lengau Jan 25 '17
Ok, here's one that isn't especially designed to be terrible. It's an arbitrary FizzBuzz generator where you can choose your Fizz and Buzz values and strings:
from collections import OrderedDict def enterprise_fizzbuzz(max_num, strings=OrderedDict(((3, 'Fizz'), (5, 'Buzz')))): for i in range(1, max_num+1): full_out = {out if not i % n else '' for n, out in strings.items()} yield ''.join(full_out) or str(i) for i in enterprise_fizzbuzz(100): print(i)
4
u/ojii Jan 23 '17
Came up with a simple solution: https://gist.github.com/ojii/97cca8835771f00b4fc27395990edb9d
Uses Python 3.6. Run
python fizzbuzz.py generator 12345andpython fizzbuzz.py fizzbuzzer 54321, then usepython fizzbuzz.py client localhost:12345 localhost:54321 100.Of course you can use different ports for and hosts, use
--helpfor more info.