My for-real-not-humor implementation: def fizzbuzz(i, n): while i
Unpythonic Python
71–80 of 156 posts
Re: Unpythonic Python
#72Earlier quoted context omitted.
Here's mine: ['Fizz' * (not bool(i % 3)) + 'Buzz' * (not bool(i % 5)) for i in xrange(1,101) if i % 3 == 0 or i % 5==0] EDIT: I hate lambdas. Also I'm not saying the above is pretty or a good idea, but I had some fun.
Mine with some itertools abuse :) from itertools import cycle , izip [(f + b or x) for f, b, x in izip(cycle([''] * 2 + ['Fizz']), cycle([''] * 4 + ['Buzz']), range(1, 100))] Edit: not sure how to post code in comments. e.g. * is lost
Re: Unpythonic Python
#73Earlier quoted context omitted.
Here's mine: ['Fizz' * (not bool(i % 3)) + 'Buzz' * (not bool(i % 5)) for i in xrange(1,101) if i % 3 == 0 or i % 5==0] EDIT: I hate lambdas. Also I'm not saying the above is pretty or a good idea, but I had some fun.
Mine with some itertools abuse :) from itertools import cycle , izip [(f + b or x) for f, b, x in izip(cycle([''] * 2 + ['Fizz']), cycle([''] * 4 + ['Buzz']), range(1, 100))] Edit: not sure how to post code in comments. e.g. * is lost
Re: Unpythonic Python
#74The superfluous semicolons on line endings in the C example gave me a chuckle.
Format strings feel like C. And not the fun parts of C.
Re: Unpythonic Python
#75One 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)]
['FizzBuzz' if 0 == n % 15 else 'Fizz' if 0 == n % 3
else 'Buzz' if 0 == n % 5 else str(n) for n in range(1,101)]Re: Unpythonic Python
#76It's the equivalent of saying "Ze fish in le river" is "Frenchish English". Ha ha, stupid French people, right?
It's easy to make fun of programming languages like this, but what does that teach us? Aside from to mock what is different than what we use right now?
Re: Unpythonic Python
#77Earlier quoted context omitted.
Something annoying about python is that the word "lambda" is so long for what are supposed to be one-off functions. You have f=lambda x,y=1,a='Fizz',b='Buzz': ... But it's actually shorter to say def f(x,y=1,a='Fizz',b='Buzz'): ... I much prefer Haskell's \x -> x Style lambdas.
So what's if it's shorter. You'll run out of screen? You'll wear down your keyboard? If you don't like typing it, have a macro. Otherwise, as Objective C shows, the verbosity of code is not relevant at all. It's what it says and what it does, which is.
And access modifiers are + and - rather than static and nonstatic.
Objective-C definitely isn't above using lightweight syntax. Where it favours verbosity is APIs.
Re: Unpythonic Python
#78If free version.
t = {}
t[0,0] = lambda x: x
t[1,0] = lambda x: "Fizz"
t[0,1] = lambda x: "Buzz"
t[1,1] = lambda x: "FizzBuzz"
def tests(x):
return (x % 3 == 0, x % 5 == 0)
for x in range(1,101):
print t[tests(x)](x)Re: Unpythonic Python
#79> If the number is divisible by 3, print Fizz instead of the number. If it’s divisible by 5, print Buzz. If it’s divisible by both 3 and 5, print FizzBuzz.
In a strict interpretation, the first two sentences could be seen as incorrect. If a number is divisible by 3, you cannot just print 'Fizz' and move on. You also have to check if it's divisible by 5.
Re: Unpythonic Python
#80Earlier quoted context omitted.
So what's if it's shorter. You'll run out of screen? You'll wear down your keyboard? If you don't like typing it, have a macro. Otherwise, as Objective C shows, the verbosity of code is not relevant at all. It's what it says and what it does, which is.
You realise the equivilent Objective-C syntax is ^{} right? And access modifiers are + and - rather than static and nonstatic. Objective-C definitely isn't above using lightweight syntax. Where it favours verbosity is APIs.
But I hoped you'd see beyond the direct comparison of lambdas with Objective C's blocks.
I'm saying that short code doesn't automatically translate to more readable or better code, Objective C's message syntax is the proof (if I have to be painfully specific about it).
I write a lot of JS and typing out "function" or "return" was never on the list of things I found a problem, despite I also work in C#, which uses the short => variation.