Skip to content

Instantly share code, notes, and snippets.

@alexland
Last active May 23, 2022 23:56
Show Gist options
  • Save alexland/cd68f202078cde329bda to your computer and use it in GitHub Desktop.
Save alexland/cd68f202078cde329bda to your computer and use it in GitHub Desktop.
fizz buzz test in three short lines with NumPy
'''
fizzbuzz refers to a quick test to filter applicants for programmer jobs who don't actually know how to code.
(see eg, http://imranontech.com/2007/01/24/using-fizzbuzz-to-find-developers-who-grok-coding/)
"pass in a sequence of integers from 1 to 100;
for integers that are multiples of three: print “fizz”
for integers that are multiples of five print “buzz”
for integers that are multiples of five AND three print "fizzbuzz"
for remaining integers, print the integer value
'''
import numpy as NP
x = NP.arange(1, 101)
cnd_list = [ (x%3==0) & (x%5==0), x%3==0, x%5==0, x ]
choice_list = ["fizzbuzz", "fizz", "buzz", x]
res = NP.select(cnd_list, choice_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment