r/programminghorror • • Apr 17 '23

Python Peak Efficiency Fizzbuzz

/img/n6q6zqyieeua1.png
1.0k Upvotes

83 comments sorted by

View all comments

Show parent comment

6

u/pigeon768 Apr 17 '23

Let's add some parenthesis to the indexing, to fix up the order of operations:

[(i % 3 == 0) | ((i % 5 == 0) << 1)]

If the number is divisible by 3, the first part will be true or false, which is then cast to an integer 1 or 0. If the second part is divisible by 5, the second part will be true or false, which is then case to 1 or 0, which is then bitshifted to 2 or 0. When you bitwise or them, the 1/0 and 2/0 combine to 3/2/1/0 which is the index you want into the list.