Created
July 8, 2019 19:19
-
-
Save llzes/eaa7385e94c6476234881dc61ad3859a to your computer and use it in GitHub Desktop.
Fizzbuzz with no if-statements and loops
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(num): | |
# i, 3, 5, 15, 7, 21, 35, 105, 11, 33, 77, 165, 231, 385, 1155 | |
outcomes = [str(num), "bun", "cheese", "buncheese", "cake", "buncake", "cheesecake", "buncheesecake", "fluff", "bunfluff", "cheesefluff", "buncheesefluff", "cakefluff", "buncakefluff", "cheesecakefluff", "buncheesecakefluff"] | |
# 0b01 = 1 in binary; &1 = %2, to zero out all bits except for the right most to see if it's a non empty number | |
divByBun = 1 - ( ( (num%3) | (num%3>>1) ) & (0b01) ) | |
divByCheese = (1 - ( ( (num%5) | (num%5>>1) | num%5>>2 ) & (0b01) ) ) * 2 | |
divByCake = (1- ( ( (num%7) | (num%7>>1) | num%7>>2 ) & (0b01) ) ) * 4 | |
divByFluff = (1- ( ( (num%11) | (num%11>>1) | num%11>>2 | num%11>>3 ) & (0b01) ) ) * 8 | |
i = divByBun ^ divByCheese ^ divByCake ^ divByFluff | |
return outcomes[i] | |
# This will return "buncheesecakefluff" | |
print fizzbuzz(1155) | |
# Proof of concept (exception to the rules if used)! | |
# for i in range(1,1157): | |
# print fizzbuzz(i) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment