So, I just learned about the Fizz Buzz test that is designed to weed out CS applicants that don't know anything. It's pretty simple:
Write a program to do the following:
1. For any number divisible by 3, print "Fizz"
2. For any number divisible by 5, print "Buzz"
3. For any number divisible by 3 and 5, print "FizzBuzz"
4. Do this in order for all numbers between 1 and 100, inclusive
Simple, right?
Well, it turns out that a shocking number of applicants can't do this at all, and even more can't do it right. Ok, I believe that. There are a lot of people that get through CS programs that aren't cut out for the discipline.
What is most shocking to me, however, is how many different websites out there offer a solution that follows the following format:
For each int i from 1 to 100 (including 100):
if i is divisible by 3:
print "Fizz"
else if i is divisible by 5:
print "Buzz"
else if i is divisible by 3 and i is divisible by 5:
print "FizzBuzz"
/facepalm, am I right? If you can't see what's wrong with this, and you are a CS person, you need to quit your job/school and go learn groundskeeping.
By the way, my solution in C:
for( int i=1; i<=100; i++ ) printf( "%s%s\n", i%3?"":"Fizz", i%5?"":"Buzz" );
C++
for( int i=1; i<=100; i++ ) cout << i%3?"":"Fizz" << i%5?"":"Buzz" << endl;
Python:
print '\n'.join( [ ( ( i%3 and ' ' or 'Fizz' ) + ( not i%5 and 'Buzz' or ' ' ) ).strip() for i in range(1,101) ] )
Python (second method)
print '\n'.join( [ '%s%s' % ( not i%3 and 'Fizz' or '', not i%5 and 'Buzz' or '' ) for i in range(1,101) ] )
Python ( third method)
print '\n'.join( [ '%s%s' % ( '' if i%3 else 'Fizz', '' if i%5 else 'Buzz' ) for i in range(1,101) ] )
P.S. The python expression was trickier, so I had fun with three different approaches.
P.P.S Python 3.0 uses print as a function, so it could be included in the list comprehension and eliminate the needed join() call
Friday, May 6, 2011
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment