One of the solutions in the comments I found quite pythonic and concise. Somehow people have it in their heads that "Pythonic" means long-winded. And yes, you have to read the code and think for a second to understand it, but that's no crime. [(not x % 3) * 'Fizz' + (not x % 5) * 'Buzz' or x for x in range(1, 101)]
Unpythonic Python
91–100 of 156 posts
Re: Unpythonic Python
#92Re: Unpythonic Python
#93 n,f,b= 100,3,5
a = [str(i) for i in range(1,n+1)]
for i in range(n/f):
a[(i+1)*(f)-1] = 'Fizz'
for i in range(n/b):
a[(i+1)*(b)-1] = 'Buzz'
for i in range(n/(f*b)):
a[(i+1)*(f*b)-1] = 'FizzBuzz'Re: Unpythonic Python
#94The superfluous semicolons on line endings in the C example gave me a chuckle.
He forgot one on the "i += 1" (which would be i++ in a for loop but Python doesn't have that operator). But that whole line is weird, because I think the C example should use a "for" loop, not increment the main counter at the top of a while loop, even if it has to use "for i in range(1, 101)" like in pythonic python. A C programmer like me (who loves python too) would immediately think "for (i = 1; i <= 100; i++)" f…
Re: Unpythonic Python
#95The value in FizzBuzz is the iteration process. What's the first step? Well, you probably make a stream of numbers. And then a set of if blocks to test and return strings. Why not keep going? What happens if you want more fizz buzz strings? Does the giant if-or statement seem a little unwieldy? Okay, pull the rules out and see if that's better. Is it easier to test now that it's a function and not a little stateful o…
Re: Unpythonic Python
#96 for (i=1; i");
}Re: Unpythonic Python
#97JS version for funsies. I didn't like the idea of specifically checking for simultaneous mod3 and mod5, so I made this. for (i=1; i "); }
Re: Unpythonic Python
#98 fizzbuzz = ['FizzBuzz', None, None, 'Fizz', None, 'Buzz', 'Fizz', None,
None, 'Fizz', 'Buzz', None, 'Fizz', None, None]
for x in xrange(1, 101):
print fizzbuzz[x % 15] or x
The second is unique in that there are no conditionals at all. (Not efficient, especially for large numbers) fizzbuzz = ['FizzBuzz', 1, 1, 'Fizz ', 1, 'Buzz ', 'Fizz ',
1, 1, 'Fizz ', 'Buzz ', 1, 'Fizz ', 1, 1]
for x in xrange(1, 101):
print str(fizzbuzz[x % 15] * x)[:8]Re: Unpythonic Python
#99Earlier quoted context omitted.
>>> let = lambda File " ", line 1 let = lambda ^ SyntaxError: invalid syntax Am I misunderstanding you?
>>> let = lambda x: 2*x >>> print let(4) 8 Edit: Awesome, downvoted for helping.
print expensive_computation(), expensive_computation()
You can do: (lambda x: print x, x)(expensive_computation())
Which would be equivalent to, with an imaginary let syntax: (let x be expensive_computation(): print x, x)Re: Unpythonic Python
#100When you write too much Haskell, your Python code starts to look like this: print('\n'.join( 'FizzBuzz' if x%5==0 and x%3==0 else 'Fizz' if x%3==0 else 'Buzz' if x%5==0 else str(x) for x in range(1, 101))) I would really like to have a "let" expression in Python to avoid having to write a new function with a def statement when you could get away with a simple lambda or generator expression.
def let(x):
yield x
And use it by "iterating": ...
for x in range(1, 101)
for divisible_by_5 in let(x % 5 == 0)
Still not as nice as having actual syntax for it.