12 ms·
Playing code golf with Python, this is the shortest I could get: https://gist.github.com/tghw/3702360 https://gist.github.com/tghw/3702360 Definitely a little
by tghw 13y ago
Playing code golf with Python, this is the shortest I could get:
https://gist.github.com/tghw/3702360 https://gist.github.com/tghw/3702360
Definitely a little unpythonic.
- pcmonk 13y agoIn the comments, someone gave this nice solution: for x in range(100): print x%3/2*'Fizz'+x%5/4*'Buzz' or x+1
- segmondy 13y agoThat's more like it! First time I heard about Fizzbuzz, I solved it in PHP. I stay amazed at the 100 lines solutions. for ($i = 1; $i <= 100; $i++) { $str = str_repeat('Fizz', !($i%3)&1) . str_repeat('Buzz', !($i%5)&1); echo $str ? "$str\n" : "$i\n"; }
- gamegoblin 13y agoSomething 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.
- ot 13y ago> But it's actually shorter It's not: with def() you have to explicitly write return, while with lambda it is implicit.
- gamegoblin 13y agoYou don't need a return, just a print (since we are doing FizzBuzz). For tghw's solution to work you'd have to have a print outside of the function call (since you can't print from a lambda). With the def, you can print inside of the function (and don't need a return, since you're just printing).
- kilink 13y agoA minor quibble, but in Python 3 you can print in a lambda because print is no longer a keyword.
- gamegoblin 13y agoAs a non-Python 3 user, I just opened up the python 3 REPL to try something I always find myself wanting to do in tiny little python scripts: map(print, some_list) Unfortunately, it returned a map object, which I guess is also new in Python 3 (I assume it's a lazy application device).
- kilink 13y agoYeah, the map builtin is essentially itertools.imap in python 3.
- benesch 13y agoYou can (ab)use iterable unpacking in Py3k, now that print's a real function: print(*[1,2,3], sep='\n')
- wting 13y agoUse `list()` to force eager evaluation with Python 3 map / filter.
- omaranto 13y agoI use this function to force evaluation of elements in a generator: def do(gen): for x in gen: pass It evaluates all elements of a generator with storing them in memory. If you don't mind generating some garbage, you can just use the list function instead.
- maxerickson 13y agoFor python golf, exec and string multiplication is usually a good strategy: https://twitter.com/jooon/status/25054586175 https://twitter.com/jooon/status/25054586175 But that isn't a whole lot shorter than the solution pcmonk posted in the thread.
- jowiar 13y agoI generally don't particularly like giving punctuation meaning that, well, isn't punctuating. The bottleneck when writing code is almost never keypresses. I think CoffeeScript gets it probably the most right of languages I've worked with, in that the choice of punctuation makes intuitive sense... "Hey! It's an Arrow. It goes from here to here". FWIW, I don't like the word "lambda" either... Naming things with one letter are generally a bad idea that we'd do better to divest ourselves of, but there is a bit of legacy to deal with. Either way, they make a field (and statistics is a major offender here, with p-values and t-tests and whatnot), terribly inaccessible.
- gamegoblin 13y agoThe reasoning behind using '\' for lambda in Haskell was because it sort of looks like a lambda character ( λ vs \ ) if you squint hard enough.
- tormeh 13y agoThat's just horrible. I don't know what more to say about it than that. How did anyone think this would be a good idea on balance?
- gamegoblin 13y agoMmm I suppose it's really just preference. I love that syntax. But I guess there's no arguing about taste.
- tormeh 13y agoWell, it's typing-friendly but not (re)learning-friendly. I guess it depends how you weigh those concerns.
- marcosdumay 13y agoIt's also very reading friendly. I normally forgive languages being hard to learn if the bring enough utility.
- mantrax4 13y agoSo 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.
- ajanuary 13y agoYou 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.
- mantrax4 13y agoI realize it, right. 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.
- ajanuary 13y agoThe parent wasn't saying code should be short, they were saying the syntax for lambdas should be short. Yes, short code doesn't automatically translate to readible code. But neither does long code. You can't wheel out objective-c's fondness for long self-documenting message names as self-evident proof that the syntax for lambdas must be verbose. Long message names are good because they let you clearly describe what is otherwise a big black box of unknown behaviour. The syntax for a lambda will always be a lambda, so it doesn't need to be spelt out explicitly each time. Shorter syntaxes such as used by Haskell are easier to visually pattern match on and so can actually aid comprehension. It's not as simple as longer is always better and anyone who disagrees is obviously foolish and lazy.
- hcarvalhoalves 13y agoThe jury is still out on whether this is a good thing or not. IMHO, the objective of the crippled `lambda` really is to make the programmer refactor into reusable functions instead of having λs littered everywhere, which would hurt maintainability.
- Silhouette 13y agoOne of the most effective ways to hurt maintainability of code written in any given programming language is to try to make some language features artificially awkward to use in the hope that programmers will then do something else. When has that ever worked? IMHO, if a feature is useful enough to have in the language, it deserves a good implementation. If it isn't, it shouldn't be there at all. And if it is useful sometimes but doesn't fit well in other situations, providing better alternatives in those situations is a more effective way to avoid dubious code than just artificially crippling the entire feature to discourage its use.
- Foxboron 13y agoMine is shorter i believe? https://gist.github.com/Foxboron/6643154 https://gist.github.com/Foxboron/6643154
- famousactress 13y agoHere'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.
- deleted 13y ago[deleted]
- lazyfunctor 13y agoMine 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
- deleted 13y ago[deleted]
- ggchappell 13y agoNicely done. > Edit: not sure how to post code in comments. e.g. * is lost Put a couple of blanks before each line. This displays it in a monospace font, too.
- deleted 13y ago[deleted]
- placeybordeaux 13y agoShorter and more pythonic, but not one per line: ' '.join(["FizzBuzz" if n%15==0 else "Fizz" if n%3==0 else "Buzz" if n%5==0 else str(n) for n in range(1,101)])
- okwa 13y agojoin on '\n' instead of ''. Problem solved.
- danbmil99 13y agohttps://gist.github.com/gitorgstars/10361634 https://gist.github.com/gitorgstars/10361634