Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

[deleted]


noice, here's my version

    print("\n".join([("FIZZ"*(i%3==0)+"BUZZ"*(i%5==0)) or str(i) for i in range(1,101)]))


I would hope never to find a bomb like that in the code I'm maintaining.


Really? I find that one line far more pleasant than the Python example above it, with variables being assigned, etc. Both are quite clear in their intention, I think, assuming you recognize "join".

I don't even recognize the language of the one-liner, but it makes perfect sense to me as a reader. It looks like a Perlish solution, but the string method is strange, I think. Maybe Perl 6 or Ruby?


The one-liner is valid Python. It's slightly non-idiomatic in that it uses a list-comprehension where a generator expression would do, and uses range instead of xrange (in Python 2.x; in 3.x range is the idiomatic alternative).


it's also non-idomatic in that is uses string-multiplication and ==0 where an "if not" ternary expression would be much cleaner.


Yes, a ternary would be clearer, to me. I wasn't sure what was happening with the * (and thought maybe it was some use of the "whatever star" in Perl 6, when I was thinking it might be Perl 6, though it doesn't look like any use of the whatever star I've seen), but I just assumed it made sense to someone who knew the language well. It's been a decade since I worked in Python with any regularity.


I would probably use a named function call in real code, but the 'text * (expr == 0)' idiom is pretty clear to me as a way to conditionally include text based on whether a condition is true or not.


It's Python as well.


I found the one-liner perfectly readable. List comprehensions, short-circuiting OR and str.join are pretty idiomatic Python.


Similar:

  print '\n'.join(['FizzBuzz' if x % 15 == 0
      else 'Fizz' if x % 3 == 0
      else 'Buzz' if x % 5 == 0
      else str(x) for x in range(1, 101)])


Starting now with this programming thing, right?

This is straight poser code -- inadvisable in any real life situation, and not that robust either.


slightly shorter javascript:

  for (var i=0; i++<100;) console.log((!(i%3)&&'Fizz'||'')+(!(i%5)&&'Buzz'||'')||i);




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: