Tuesday, January 22, 2013

Fizzbizz

I hear this is a classic interview question. It demonstrates basic competency with 'for' loops. Shown here in Python.


#fizzbizz
#print all #s 1-100
#if a number is a multiple of 3, sub fizz
#if num is multiple of 5, sub bizz
#if multiple of both, print fizzbizz


for i in range(1,101):
if i%3 == 0:
print "fizz",
if i%5 == 0:
print "bizz",
if i%3 and i%5:
print i,
print ""

No comments:

Post a Comment