This is extremely unpythonic and an ugly hack. It's ok for a quick script never to be used by anyone other than you, but bad code to demonstrate for a software engineering interview. From import this:
Simple is better than complex.
Sparse is better than dense.
Readability counts.
First, comments that lie are much worse than no comments at all. Your first comment lies; range(1,101) goes from 1 to 100 inclusive, not 0. Code that you don't need to comment is much cleaner than code that you need to comment. For a trivial piece of logic like this, don't mess it up by relying on python casting i%3 to a bool, applying not to it, and casting it back to an int type for string multiplication, as well as relying on or precedence being lower than string concatenation/multiplication.
for i in range(1, 101):
if i % 15 == 0:
print "FizzBuzz"
elif i % 3 == 0:
print "Fizz"
elif i % 5 == 0:
print "Buzz"
else:
print i
Not counting indentation/whitespace this clear obvious solution is 130 characters. Yours is 200 characters due to the terse code necessitating comments.
View all comments
Show parent comment
23
u/djimbob Jan 17 '14 edited Jan 17 '14
This is extremely unpythonic and an ugly hack. It's ok for a quick script never to be used by anyone other than you, but bad code to demonstrate for a software engineering interview. From
import this:First, comments that lie are much worse than no comments at all. Your first comment lies;
range(1,101)goes from 1 to 100 inclusive, not 0. Code that you don't need to comment is much cleaner than code that you need to comment. For a trivial piece of logic like this, don't mess it up by relying on python castingi%3to abool, applyingnotto it, and casting it back to aninttype for string multiplication, as well as relying onorprecedence being lower than string concatenation/multiplication.Not counting indentation/whitespace this clear obvious solution is 130 characters. Yours is 200 characters due to the terse code necessitating comments.