# Obfuscated Fizzbuzz

### David Watson

david@davidwatson.org

Slides
https://github.com/davidthewatson/obfuscated_fizzbuzz

Original Source and Analysis
https://github.com/davidthewatson/fizzbuzz

Originally delivered as a lightning talk to Pittsburgh Python

# Co-opted

### into an interview question

``````
For the numbers from 1 to 100
If the number is evenly divisible by 3 print fizz
If the number is evenly divisible by 5 print buzz
If the number is evenly divisible by 3 and 5 print fizzbuzz
Else print the number
``````

# Obfuscated FizzBuzz

### Lambdas, closures, nested generator expressions

``````
print(*((lambda x=x: ''.join(chr(c) for c in (102, 105)) + (2 * chr(122)) + ''.join(chr(c) for c in (98, 117)) + (2 * chr(122)) + '\n' if x % (30 >> 1) == 0 else ''.join(chr(c) for c in (102, 105)) + (2 * chr(122)) + '\n' if x % (6 >> 1) == 0 else ''.join(chr(c) for c in (98, 117)) + (2 * chr(122)) + '\n' if x % (10 >> 1) == 0 else str(x) + '\n')() for x in range(1, 101)))
``````

# Byzantine FizzBuzz

### Less than ten lines of python

``````
from itertools import chain
all = list(range(1, 101))
divisible_by_3 = dict((x, 'Fizz') for x in filter(lambda x: x % 3 == 0, all))
divisible_by_5 = dict((x, 'Buzz') for x in filter(lambda x: x % 5 == 0, all))
divisible_by_3_and_5 = dict((x, 'FizzBuzz') for x in
filter(lambda x: x % 3 == 0 and x % 5 == 0, all))
remainder = dict((x, x) for x in [x for x in all
if not x in divisible_by_3
and not x in divisible_by_5
and not x in divisible_by_3_and_5])
all_dict = dict(chain(divisible_by_3.items(),
divisible_by_5.items(),
divisible_by_3_and_5.items(),
remainder.items()))
for x in all:
print(all_dict[x])
``````

# Canonical Fizzbuzz

### The simplest thing that could possibly work

``````
for i in range(1, 101):
if i % 15 == 0:
print('FizzBuzz')
elif i % 3 == 0:
print('Fizz')
elif i % 5 == 0:
print('Buzz')
else:
print(i)
``````

# All three solutions

### produce expected output and perform similarly

``1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz 26 Fizz 28 29 FizzBuzz 31 32 Fizz 34 Buzz Fizz 37 38 Fizz Buzz 41 Fizz 43 44 FizzBuzz 46 47 Fizz 49 Buzz Fizz 52 53 Fizz Buzz 56 Fizz 58 59 FizzBuzz 61 62 Fizz 64 Buzz Fizz 67 68 Fizz Buzz 71 Fizz 73 74 FizzBuzz 76 77 Fizz 79 Buzz Fizz 82 83 Fizz Buzz 86 Fizz 88 89 FizzBuzz 91 92 Fizz 94 Buzz Fizz 97 98 Fizz Buzz``